home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / futils / futils~1 / src / text11s.zoo / text1.1 / lib / regex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-02  |  167.9 KB  |  5,586 lines

  1. /* Extended regular expression matching and search library.
  2.    Version 0.1.
  3.    Copyright (C) 1985, 89, 90, 91 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19.  
  20. /* To test, compile with -Dtest.  This Dtestable feature turns this into
  21.    a self-contained program which reads a pattern, describes how it
  22.    compiles, then reads a string and searches for it.
  23.  
  24.    On the other hand, if you compile with both -Dtest and -Dcanned you
  25.    can run some tests we've already thought of.  */
  26.  
  27.  
  28. #ifdef REGEX_MALLOC
  29.  
  30. #define REGEX_ALLOCATE malloc
  31. #define REGEX_REALLOCATE(source, size) (realloc (source, size))
  32.  
  33. #else /* not REGEX_MALLOC  */
  34.  
  35.  
  36. /* Make alloca work the best possible way.  */
  37. #ifdef __GNUC__
  38. #define alloca __builtin_alloca
  39. #else
  40. #ifdef sparc
  41. #include <alloca.h>
  42. #else 
  43. #ifdef _AIX
  44.   #pragma alloca
  45. #else /* not __GNUC__ or sparc or _AIX */
  46. char *alloca ();
  47. #endif  /* _AIX  */ 
  48. #endif  /* sparc  */ 
  49. #endif  /* not  __GNUC__  */
  50.  
  51. /* Still not defined (REGEX_MALLOC)  */
  52.  
  53. #define REGEX_ALLOCATE alloca
  54.  
  55. /* Requires a `void *destination' declared.  */
  56. #define REGEX_REALLOCATE(source, size)                    \
  57.   (destination = alloca (size),                        \
  58.    bcopy (source, destination, size),                    \
  59.    destination)
  60.  
  61. #endif /* not defined (REGEX_MALLOC)  */
  62.  
  63.  
  64.  
  65. #ifdef emacs
  66.  
  67. /* The `emacs' switch turns on certain special matching commands
  68.   that make sense only in emacs. */
  69.  
  70. #include "config.h"
  71. #include "lisp.h"
  72. #include "buffer.h"
  73. #include "syntax.h"
  74.  
  75. /* Emacs uses `NULL' as a predicate.  */
  76. #undef NULL
  77.  
  78.  
  79. #else  /* not emacs */
  80.  
  81.  
  82. #include <sys/types.h>  /* POSIX types.  */
  83.  
  84. #if defined (USG) || defined (POSIX) || defined (STDC_HEADERS)
  85. #ifndef BSTRING
  86. #include <string.h>
  87. #define bcopy(s,d,n)    memcpy ((d), (s), (n))
  88. #define bcmp(s1,s2,n)    memcmp ((s1), (s2), (n))
  89. #define bzero(s,n)    memset ((s), 0, (n))
  90. #endif /* not BSTRING  */
  91. #endif /* USG or POSIX or STDC_HEADERS  */
  92.  
  93. #if defined (POSIX) || defined (STDC_HEADERS)
  94. #include <stdlib.h>
  95. #else
  96. #ifdef __STDC__
  97. void *malloc (size_t);
  98. void *realloc (void *, size_t);
  99. #else /* not __STDC__  */
  100. char *malloc ();
  101. char *realloc ();
  102. #endif /* not __STDC__  */
  103. #endif  /* not (POSIX or STDC_HEADERS) */
  104.  
  105.  
  106.  
  107. /* How many characters in the character set.  */
  108. #define CHAR_SET_SIZE  256
  109.  
  110.  
  111. /* Define the syntax stuff, so we can do the \<, \>, etc.  */
  112.  
  113. /* This must be nonzero for the wordchar and notwordchar pattern
  114.    commands in re_match_2.  */
  115. #ifndef Sword 
  116. #define Sword 1
  117. #endif /* not Sword  */
  118.  
  119. #define SYNTAX(c) re_syntax_table[c]
  120.  
  121.  
  122. #ifdef SYNTAX_TABLE
  123.  
  124. extern char *re_syntax_table;
  125.  
  126. #else /* not SYNTAX_TABLE */
  127.  
  128. static char re_syntax_table[CHAR_SET_SIZE];
  129.  
  130. static void
  131. init_syntax_once ()
  132. {
  133.    register int c;
  134.    static int done = 0;
  135.  
  136.    if (done)
  137.      return;
  138.  
  139.    bzero (re_syntax_table, sizeof re_syntax_table);
  140.  
  141.    for (c = 'a'; c <= 'z'; c++)
  142.      re_syntax_table[c] = Sword;
  143.  
  144.    for (c = 'A'; c <= 'Z'; c++)
  145.      re_syntax_table[c] = Sword;
  146.  
  147.    for (c = '0'; c <= '9'; c++)
  148.      re_syntax_table[c] = Sword;
  149.  
  150.    re_syntax_table['_'] = Sword;
  151.  
  152.    done = 1;
  153. }
  154.  
  155. #endif /* not SYNTAX_TABLE */
  156. #endif /* not emacs */
  157.  
  158. /* We write fatal error messages on standard error.  */
  159. #include <stdio.h>
  160.  
  161. /* isalpha(3) etc. are used for the character classes.  */
  162. #include <ctype.h>
  163.  
  164. /* Sequents are missing isgraph.  */
  165. #ifdef sequent
  166. #define ISGRAPH_MISSING
  167. #endif
  168.  
  169. #ifdef ISGRAPH_MISSING
  170. #define isgraph(c) (isprint (c) && !isspace (c))
  171. #endif
  172.  
  173.  
  174. /* Get the interface, including the syntax bits.  */
  175. #include "regex.h"
  176.  
  177. /* We will need this constant several times.  */
  178. #define BYTEWIDTH 8
  179.  
  180.  
  181.  
  182.  
  183. /* These are the command codes that appear in compiled regular
  184.    expressions, one per byte.  Some command codes are followed by
  185.    argument bytes.  A command code can specify any interpretation
  186.    whatsoever for its arguments.  Zero-bytes may appear in the compiled
  187.    regular expression.
  188.  
  189.    The value of `exactn' is needed in search.c (search_buffer) in emacs.
  190.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  191.    `exactn' we use here must also be 1.  */
  192.  
  193. enum regexpcode
  194.   {
  195.     no_op=0,
  196.     exactn=1,        /* Followed by one byte giving n, then by n
  197.                            literal bytes.  */ 
  198.     begline,        /* Fail unless at beginning of line.  */
  199.     endline,        /* Fail unless at end of line.  */
  200.     endline_in_repeat,    /* If in trailing position, turn into an endline, 
  201.                otherwise, turn into a no_op.  This should
  202.                            never end up in the final compiled pattern!  */
  203.     endline_before_newline,/* If after an endline, don't that endline turn into
  204.                an exactn for '$' when RE_CONTEXT_INDEP_ANCHORS
  205.                            is set.  Should never end up in the compiled
  206.                            pattern! */
  207.     repeated_endline_before_newline, /* A combination of above two.  */
  208.     no_pop_jump,    /* Followed by two byte relative address to
  209.                which to jump.  */ 
  210.     jump_past_next_alt, /* Same as no_pop_jump, but don't jump if the
  211.                    current group (the largest-numbered active
  212.                            one) hasn't matched anything.  */
  213.     on_failure_jump,    /* Followed by two byte relative address of 
  214.                            place to resume at in case of failure.  */
  215.     pop_failure_jump,    /* Throw away latest failure point and then jump to 
  216.                             address.  */
  217.     maybe_pop_jump,
  218.                         /* Like jump but change to pop_failure_jump 
  219.                           only if know won't have to backtrack to
  220.                           match; otherwise change to no_pop_jump.
  221.                           This is used to jump back to the
  222.                           beginning of a repeat.  If what follows
  223.                           this jump clearly won't match what the
  224.                           repeat does, such that we can be sure
  225.                           that there is no use backtracking out of
  226.                           repetitions already matched, then we
  227.                           change it to a pop_failure_jump.  */
  228.     dummy_failure_jump,    /* Jump, and push a dummy failure point. This 
  229.                            failure point will be thrown away if an
  230.                            attempt is made to use it for a failure. A
  231.                            `+' construct makes this before the first
  232.                            repeat.  Also use it as an intermediary kind
  233.                            of jump when compiling an alternative.  */
  234.     succeed_n,        /* Used like on_failure_jump except has to
  235.                            succeed n times; The two-byte relative
  236.                            address following it is useless until then.
  237.                            The address is followed by two bytes
  238.                            containing n.  */
  239.     no_pop_jump_n,    /* Similar to no_pop_jump, but jump n times
  240.                            only; also the relative address following is
  241.                            in turn followed by yet two more bytes
  242.                            containing n.  */
  243.     set_number_at,    /* Set the following relative location (two
  244.                            bytes) to the subsequent (two-byte) number.  */
  245.     anychar,        /* Matches any (more or less) character.  */
  246.     charset,        /* Matches any one char belonging to specified set.
  247.                            First following byte is number of bitmap
  248.                            bytes.  Then come bytes for a bitmap saying
  249.                            which chars are in.  Bits in each byte are
  250.                            ordered low-bit-first.  A character is in the
  251.                            set if its bit is 1.  A character too large
  252.                            to have a bit in the map is automatically not
  253.                            in the set.  */
  254.     charset_not,    /* Same parameters as charset, but match any
  255.                            character that is not one of those specified.  */
  256.     start_memory,    /* Start remembering the text that is matched, for
  257.                            storing in a memory register.  Followed by
  258.                            one byte containing the register number.
  259.                            Register numbers will be in the range 0
  260.                            through one less than the pattern buffer's
  261.                            re_nsub field.  */
  262.     stop_memory,    /* Stop remembering the text that is matched
  263.                            and store it in a memory register.  Followed
  264.                            by one byte containing the register number.
  265.                            Register numbers will be in the range 0
  266.                            through one less than the pattern buffer's
  267.                            re_nsub field.  */
  268.     duplicate,        /* Match a duplicate of something remembered.
  269.                            Followed by one byte containing the register
  270.                            number.  */
  271.     before_dot,        /* Succeeds if before point.  */
  272.     at_dot,        /* Succeeds if at point.  */
  273.     after_dot,        /* Succeeds if after point.  */
  274.     begbuf,        /* Succeeds if at beginning of buffer.  */
  275.     endbuf,        /* Succeeds if at end of buffer.  */
  276.     wordchar,        /* Matches any word-constituent character.  */
  277.     notwordchar,    /* Matches any char that is not a word-constituent.  */
  278.     wordbeg,        /* Succeeds if at word beginning.  */
  279.     wordend,        /* Succeeds if at word end.  */
  280.     wordbound,      /* Succeeds if at a word boundary.  */
  281.     notwordbound,    /* Succeeds if not at a word boundary.  */
  282.     syntaxspec,        /* Matches any character whose syntax is specified.
  283.                            followed by a byte which contains a syntax
  284.                            code, e.g., Sword.  */
  285.     notsyntaxspec    /* Matches any character whose syntax differs from
  286.                            that specified.  */
  287.   };
  288.  
  289.  
  290.  
  291. #ifdef CHAR_UNSIGNED    /* for, e.g., IBM RT */
  292. #define SIGN_EXTEND_CHAR(c) (((c)^128) - 128) /* As in Harbison and Steele.  */
  293. #endif
  294. #ifndef SIGN_EXTEND_CHAR
  295. #define SIGN_EXTEND_CHAR    /* As nothing.  */
  296. #endif
  297.  
  298.  
  299.  
  300. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  301.  
  302. #define STORE_NUMBER(destination, number)                \
  303.   do {(destination)[0] = (number) & 0377;                \
  304.    (destination)[1] = (number) >> 8;                     \
  305.   } while (0)
  306.  
  307.  
  308. /* Same as STORE_NUMBER, except increment the destination pointer to
  309.    the byte after where the number is stored.  Watch out that values for
  310.    DESTINATION such as p + 1 won't work, whereas p will.  */
  311.  
  312. #define STORE_NUMBER_AND_INCR(destination, number)            \
  313.   do { STORE_NUMBER(destination, number);                \
  314.     (destination) += 2;                            \
  315.   } while (0)
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322. /* Put into DESTINATION a number stored in two contingous bytes starting
  323.    at SOURCE.  */
  324.  
  325. #define EXTRACT_NUMBER(destination, source)                \
  326.   do { (destination) = *(source) & 0377;                \
  327.     (destination) += SIGN_EXTEND_CHAR (*(char *)((source) + 1)) << 8;     \
  328.   } while (0)
  329.  
  330. int
  331. extract_number (source)
  332.   unsigned char *source;
  333. {
  334.   int answer;
  335.   int i_temp = * (char *) (source + 1);
  336.   char c_temp = * (char *) (source + 1);
  337.   
  338.   i_temp = SIGN_EXTEND_CHAR (i_temp);
  339.   c_temp = SIGN_EXTEND_CHAR (c_temp);
  340.   
  341.   i_temp <<= 8;
  342.   c_temp <<= 8;
  343.  
  344.   answer = *source & 0377;
  345.   answer += (SIGN_EXTEND_CHAR (*(char *)((source) + 1))) << 8;
  346.   
  347.   return answer;
  348. }
  349.  
  350.  
  351. /* Same as EXTRACT_NUMBER, except increment the pointer for source to
  352.    point to second byte of SOURCE.  Note that SOURCE has to be a value
  353.    such as p, not, e.g., p + 1. */
  354.  
  355. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  356.   do { EXTRACT_NUMBER (destination, source);                \
  357.     (source) += 2;                             \
  358.   } while (0)
  359.  
  360.  
  361. void
  362. extract_number_and_incr (destination, source)
  363.   int *destination;
  364.   unsigned char **source;
  365.   *destination = extract_number (*source);
  366.   *source += 2;
  367. }
  368.  
  369.  
  370.  
  371. typedef enum { false = 0, true = 1 } boolean;
  372.  
  373. /* Number of failure points for which to initially allocate space
  374.    when matching.  If this number is exceeded, we allocate more space---
  375.    so it is not a hard limit.  */
  376.  
  377. #ifndef INIT_FAILURE_ALLOC
  378. #define INIT_FAILURE_ALLOC 5
  379. #endif
  380.  
  381.  
  382. /* Specify the precise syntax of regexps for compilation.  This provides
  383.    for compatibility for various utilities which historically have
  384.    different, incompatible syntaxes.
  385.  
  386.    The argument SYNTAX is a bit-mask comprised of the various bits
  387.    defined in regex.h.  */
  388.  
  389. int
  390. re_set_syntax (syntax)
  391.   int syntax;
  392. {
  393.   int ret;
  394.  
  395.   ret = obscure_syntax;
  396.   obscure_syntax = syntax;
  397.   return ret;
  398. }
  399.  
  400. /* Set by re_set_syntax to the current regexp syntax to recognize.  */
  401. int obscure_syntax = 0;
  402.  
  403.  
  404.  
  405. /* Routine used by re_compile_pattern, re_comp and regcomp.  */
  406.  
  407. #ifdef __STDC__
  408. static char *regex_compile (const char *pattern, const int size,
  409.                             const int syntax, struct re_pattern_buffer *bufp);
  410. #else
  411. static char *regex_compile ();
  412. #endif
  413.  
  414.  
  415.  
  416. /* re_compile_pattern takes a regular-expression string and converts it
  417.    into a buffer full of byte commands for matching.
  418.  
  419.    PATTERN   is the address of the pattern string.
  420.    SIZE      is the length of it.
  421.  
  422.    BUFP      is a struct re_pattern_buffer * whose pertinent fields are
  423.              mentioned below:
  424.  
  425.              It has a char * field called BUFFER which points to the
  426.              space where this routine will put the compiled pattern; the
  427.              user can either allocate this using malloc (whereupon they
  428.              should set the long field ALLOCATED to the number of bytes
  429.              malloced) or set ALLOCATED to 0 and let the routine
  430.              allocate it.  The routine may use realloc to enlarge the
  431.              buffer space.
  432.  
  433.              If the user wants to translate all ordinary elements in the
  434.              compiled pattern, they should set the char * field
  435.              TRANSLATE to a translate table, otherwise, they should set
  436.              it to 0.
  437.  
  438.              The routine sets the int field SYNTAX to the value of the
  439.              global variable `obscure_syntax'.
  440.  
  441.              It returns in the long field USED how many bytes long the
  442.              compiled pattern is.
  443.  
  444.              It returns 0 in the char field FASTMAP_ACCURATE, on
  445.              the assumption that the user usually doesn't compile the
  446.              same pattern twice and that consequently any fastmap in the
  447.              pattern buffer is inaccurate.
  448.  
  449.              In the size_t field RE_NSUB, it returns the number of
  450.              subexpressions it found in PATTERN.
  451.  
  452.    Returns 0 if the pattern was valid and an error string if it wasn't.   */
  453.  
  454.  
  455. char *
  456. re_compile_pattern (pattern, size, bufp)
  457.      const char *pattern;
  458.      const int size;
  459.      struct re_pattern_buffer *bufp;
  460. {
  461.   bufp->return_default_num_regs = (obscure_syntax & RE_ALLOCATE_REGISTERS) > 0;
  462.  
  463.   return regex_compile (pattern, size, obscure_syntax, bufp);
  464. }     
  465.  
  466.  
  467.  
  468. /* Macros for regex_compile.  */
  469.  
  470. #define CHAR_CLASS_MAX_LENGTH  6
  471.  
  472.  
  473. /* Fetch the next character in the uncompiled pattern---translating it 
  474.    if necessary.  */
  475.  
  476. #define PATFETCH(c)                            \
  477.   do {if (p == pend) goto end_of_pattern;                \
  478.     c = * (unsigned char *) p++;                    \
  479.     if (translate)                             \
  480.     c = translate[c];                             \
  481.   } while (0)
  482.  
  483. /* Fetch the next character in the uncompiled pattern, with no
  484.    translation.  */
  485.  
  486. #define PATFETCH_RAW(c)                            \
  487.   do {if (p == pend) goto end_of_pattern;                \
  488.     c = * (unsigned char *) p++;                     \
  489.   } while (0)
  490.  
  491. #define PATUNFETCH p--
  492.  
  493.  
  494. /* Pattern offset stuff.  */
  495.  
  496. #define INIT_PATTERN_OFFSETS_LIST_SIZE 32
  497.  
  498. typedef short pattern_offset_type;
  499.  
  500. typedef struct {
  501.   pattern_offset_type *offsets;
  502.   unsigned size;
  503.   unsigned avail;
  504. } pattern_offsets_list_type;
  505.  
  506. #define PATTERN_OFFSETS_LIST_PTR_FULL(pattern_offsets_list_ptr)        \
  507.   (pattern_offsets_list_ptr->avail == pattern_offsets_list_ptr->size)
  508.  
  509.  
  510. /* Anchor and op list stuff.  */
  511.  
  512. typedef pattern_offsets_list_type anchor_list_type;
  513. typedef pattern_offsets_list_type op_list_type;
  514.  
  515.  
  516.  
  517. /* Bits list declaration.  An arbitrarily long string of bits.  */
  518.  
  519. typedef struct {
  520.    unsigned *bits;
  521.    unsigned size;
  522. } bits_list_type;
  523.  
  524.  
  525. /* Bits list macros.  See below for routines.  */
  526.  
  527. #define BITS_BLOCK_SIZE (sizeof (unsigned) * BYTEWIDTH)
  528. #define BITS_BLOCK(position) ((position) / BITS_BLOCK_SIZE)
  529. #define BITS_MASK(position) (1 << ((position) % BITS_BLOCK_SIZE))
  530.  
  531.  
  532. /* Initialize BITS_LIST (of type bits_list_type) to have one bits
  533.    block.  Mostly analogous to routine init_bits_list, but, if
  534.    REGEX_MALLOC is not defined, uses `alloca' instead of `malloc'.  This
  535.    is because using malloc in re_search* or re_match* could cause core
  536.    leaks when C-g is used in Emacs, plus malloc's slower and causes
  537.    storage fragmentation.  This has to be a macro because the results of
  538.    `alloca' disappear at the end of the routine it's in.  (If for some
  539.    reason you delete this explanation, please put it in the comment for
  540.    the failure stack.)
  541.    
  542.    Return 1 if there's enough memory to do so and 0 if there isn't.  */
  543.  
  544. #define INIT_BITS_LIST(bits_list)                    \
  545.   (bits_list.bits = (unsigned *) REGEX_ALLOCATE (sizeof (unsigned)),    \
  546.    bits_list.bits == NULL                         \
  547.    ? 0                                    \
  548.    : (bits_list.size = BITS_BLOCK_SIZE,                    \
  549.       bits_list.bits[0] = 0,                        \
  550.       1))
  551.  
  552.  
  553. /* Extend BITS_LIST_PTR (of type bits_list_type) by one bits block.
  554.    Return 1 if there's enough memory to do so and 0 if there isn't.
  555.    Analogous to routine extend_bits_list, but uses alloca instead of
  556.    realloc, for reasons stated above in INIT_BITS_LIST's comment.  
  557.    
  558.    Because REGEX_REALLOCATE requires a declaration of `void
  559.    *destination', so does this.  */
  560.    
  561.  
  562. #define EXTEND_BITS_LIST(bits_list)                    \
  563.   (bits_list.bits                            \
  564.     = REGEX_REALLOCATE (bits_list.bits,                 \
  565.             bits_list.size / BYTEWIDTH             \
  566.                           + BITS_BLOCK_SIZE / BYTEWIDTH),        \
  567.    bits_list.bits == NULL                        \
  568.    ? 0                                    \
  569.    : (bits_list.size += BITS_BLOCK_SIZE,                \
  570.       bits_list.bits[(bits_list.size/BITS_BLOCK_SIZE) - 1] = 0,        \
  571.       1))
  572.  
  573.  
  574. /* Set the bit for a positive POSITION in BITS_LIST_PTR to VALUE, which,
  575.    in turn, can only be 0 or 1.
  576.  
  577.    Returns 1       if can set the bit.
  578.        0       if ran out of memory allocating (if necessary) room for it.  
  579.        value  if the value is invalid (i.e., not 0 or 1).  
  580.            
  581.            Because EXTENT_BITS_LIST requires a declaration of `void
  582.            *destination', so does this.  */
  583.    
  584. #define SET_BIT_TO_VALUE(bits_list, position, value)            \
  585.   (position > bits_list.size - 1                    \
  586.     && !EXTEND_BITS_LIST (bits_list)                    \
  587.     ? 0                                    \
  588.     : (value == 1                            \
  589.        ? (bits_list.bits[BITS_BLOCK (position)]             \
  590.             |= BITS_MASK (position), 1)                    \
  591.        : (value == 0                            \
  592.           ? (bits_list.bits[BITS_BLOCK (position)]            \
  593.              &= ~(BITS_MASK (position)), 1)                \
  594.           : value)                            \
  595.        ))
  596.  
  597.  
  598.  
  599. /* Compile stack stuff.  */
  600.  
  601. typedef struct {
  602.   pattern_offset_type laststart_offset;  
  603.   pattern_offset_type fixup_alt_jump;
  604.   pattern_offset_type regnum;
  605.   pattern_offset_type begalt_offset;
  606. } compile_stack_element;
  607.  
  608.  
  609. typedef struct {
  610.     compile_stack_element *stack;
  611.     unsigned size;
  612.     unsigned avail;            /* Offset of next open position.  */
  613.   } compile_stack_type;
  614.  
  615.  
  616. #define INIT_COMPILE_STACK_SIZE 32
  617.  
  618. #define COMPILE_STACK_EMPTY  (compile_stack.avail == 0)
  619. #define COMPILE_STACK_FULL  (compile_stack.avail == compile_stack.size)
  620.  
  621.  
  622. /* If the buffer isn't allocated when it comes in, use this.  */
  623. #define INIT_BUF_SIZE  32
  624.  
  625. /* Make sure we have at least N more bytes of space in buffer.  */
  626. #define GET_BUFFER_SPACE(n)                        \
  627.   {                                        \
  628.     while (b - bufp->buffer + (n) > bufp->allocated)            \
  629.       EXTEND_BUFFER                            \
  630.   }
  631.  
  632. /* Make sure we have one more byte of buffer space and then add C to it.  */
  633. #define BUF_PUSH(c)                            \
  634.   do {                                    \
  635.     GET_BUFFER_SPACE (1);                        \
  636.     *b++ = (char) (c);                            \
  637.   } while (0)
  638.  
  639. /* Make sure we have two more bytes of buffer space and then add C1 and 
  640.    C2 to it.  */
  641. #define BUF_PUSH_2(c1, c2)                        \
  642.   do {                                    \
  643.     GET_BUFFER_SPACE (2);                        \
  644.     *b++ = (char) (c1);                            \
  645.     *b++ = (char) (c2);                            \
  646.   } while (0)
  647.  
  648.  
  649.  
  650. #define MAX_BUF_SIZE (1L << 16)
  651.  
  652. /* Extend the buffer by twice its current size via realloc and
  653.    reset the pointers that pointed into the old block to point to the
  654.    correct places in the new one.  If extending the buffer results in it
  655.    being larger than MAX_BUF_SIZE, then flag memory exhausted.  */
  656. #define EXTEND_BUFFER                            \
  657.   {                                     \
  658.     char *old_buffer = bufp->buffer;                    \
  659.     if (bufp->allocated == MAX_BUF_SIZE)                 \
  660.       goto too_big;                            \
  661.     bufp->allocated <<= 1;                        \
  662.     if (bufp->allocated > MAX_BUF_SIZE)                    \
  663.       bufp->allocated = MAX_BUF_SIZE;                     \
  664.     bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated);    \
  665.     if (bufp->buffer == NULL)                        \
  666.       goto memory_exhausted;                        \
  667.     b = (b - old_buffer) + bufp->buffer;                \
  668.     begalt = (begalt - old_buffer) + bufp->buffer;            \
  669.     beg_interval = (beg_interval - old_buffer) + bufp->buffer;        \
  670.     if (fixup_alt_jump)                            \
  671.       fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;    \
  672.     if (laststart)                            \
  673.       laststart = (laststart - old_buffer) + bufp->buffer;        \
  674.     if (pending_exact)                            \
  675.       pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  676.   }
  677.  
  678. /* Set the bit for character C in a list.  */
  679. #define SET_LIST_BIT(c)  (b[(c) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
  680.  
  681. /* Get the next unsigned number in the uncompiled pattern.  */
  682. #define GET_UNSIGNED_NUMBER(num)                     \
  683.   { if (p != pend)                            \
  684.      {                                    \
  685.        PATFETCH (c);                             \
  686.        while (isdigit (c))                         \
  687.          {                                 \
  688.            if (num < 0)                        \
  689.               num = 0;                            \
  690.            num = num * 10 + c - '0';                     \
  691.            if (p == pend)                         \
  692.               break;                             \
  693.            PATFETCH (c);                        \
  694.          }                                 \
  695.        }                                 \
  696.     }        
  697.  
  698.  
  699. #define DO_RANGE                            \
  700.   {                                    \
  701.              /* Get untranslated range start and end characters.  */    \
  702.     char this_char = p[-2];                        \
  703.     char end;                                \
  704.                                                                            \
  705.     if (p == pend)                            \
  706.       goto invalid_range_end;                        \
  707.     PATFETCH_RAW (end);                            \
  708.     if ((syntax & RE_NO_EMPTY_RANGES) && this_char > end)        \
  709.       goto invalid_range_end;                        \
  710.     while (this_char <= end)                        \
  711.       {                                    \
  712.         SET_LIST_BIT (translate ? translate[this_char] : this_char);    \
  713.         this_char++;                            \
  714.       }                                    \
  715.     }
  716.  
  717.  
  718. #define IS_CHAR_CLASS(string)                        \
  719.    (strcmp (string, "alpha") == 0 || strcmp (string, "upper") == 0    \
  720.     || strcmp (string, "lower") == 0 || strcmp (string, "digit") == 0    \
  721.     || strcmp (string, "alnum") == 0 || strcmp (string, "xdigit") == 0    \
  722.     || strcmp (string, "space") == 0 || strcmp (string, "print") == 0    \
  723.     || strcmp (string, "punct") == 0 || strcmp (string, "graph") == 0    \
  724.     || strcmp (string, "cntrl") == 0)                    \
  725.  
  726.  
  727.  
  728. /* Subroutines for regex_compile.  */
  729.  
  730. static void store_jump (), insert_jump (), store_jump_n (),
  731.             insert_jump_n (), insert_op_2 (), remove_intervening_anchors (), 
  732.             clear_this_and_higher_levels (), increase_level (),
  733.             decrease_level (), adjust_pattern_offsets_list ();
  734.  
  735.  
  736. static unsigned record_anchor_position (), init_bits_list (),
  737.         get_level_match_status (), 
  738.                 set_this_level (), set_next_lower_level (),
  739.                 make_group_active (), make_group_inactive (),
  740.                 set_match_status_of_active_groups (), 
  741.                 get_group_match_status (), add_op (),
  742.                 init_pattern_offsets_list ();
  743.  
  744. static boolean is_in_compile_stack (), lower_levels_match_nothing (), 
  745.            no_levels_match_anything (), verify_and_adjust_endlines ();
  746.  
  747.  
  748. static char *
  749. regex_compile (pattern, size, syntax, bufp)
  750.      const char *pattern;
  751.      const int size;
  752.      const int syntax;
  753.      struct re_pattern_buffer *bufp;
  754. {
  755.   register char *b = bufp->buffer;
  756.   const char *p = pattern;
  757.   const char *pend = pattern + size;
  758.   register unsigned c, c1;
  759.   const char *p1;
  760.   unsigned char *translate = (unsigned char *) bufp->translate;
  761.   boolean enough_memory;
  762.  
  763.   /* Address of the count-byte of the most recently inserted `exactn'
  764.      command.  This makes it possible to tell whether a new exact-match
  765.      character can be added to that command or requires a new `exactn'
  766.      command.  */
  767.  
  768.   char *pending_exact = 0;
  769.  
  770.   /* Address of the place where a forward jump should go to the end of
  771.      the containing expression.  Each alternative of an `or', except the
  772.      last, ends with a forward jump of this sort.  */
  773.  
  774.   char *fixup_alt_jump = 0;
  775.  
  776.   /* Address of start of the most recently finished expression.
  777.      This tells, e. g., postfix * where to find the start of its operand.  */
  778.  
  779.   char *laststart = 0;
  780.  
  781.   /* In processing a repeat, 1 means zero matches is allowed.  */
  782.  
  783.   char zero_times_ok;
  784.  
  785.   /* In processing a repeat, 1 means many matches is allowed.  */
  786.  
  787.   char many_times_ok;
  788.  
  789.   /* Address of beginning of regexp, or inside of last group.  */
  790.  
  791.   char *begalt = b;
  792.   
  793.   /* In processing an interval, at least this many matches must be made.  */
  794.   int lower_bound;
  795.  
  796.   /* In processing an interval, at most this many matches can be made.  */
  797.   int upper_bound;
  798.  
  799.   /* Place in pattern (i.e., the {) to which to go back if the interval
  800.      is invalid.  */
  801.   const char *beg_interval = 0;
  802.   const char *following_left_brace = 0;
  803.  
  804.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  805.      where it becomes the register number to put in the stop_memory
  806.      command.  */
  807.  
  808.   int regnum = 1;
  809.  
  810.   compile_stack_type compile_stack;
  811.   anchor_list_type anchor_list;
  812.  
  813.   /* Keeps track of whether or not the pattern at a given grouping level
  814.      matches the empty string so far.  Each bit in the `bits' field of
  815.      this variable corresponds to a level, starting at level zero (i.e.,
  816.      the whole pattern) at the rightmost bit of list[0].  Level 1 is the
  817.      bit to the left of that, etc.  Additional bits that won't fit in
  818.      bits[0] are in bits[2], bits[3], etc.  */
  819.  
  820.   bits_list_type level_match_status;
  821.   unsigned current_level = 0;
  822.   
  823.   /* Does a similar thing for groups that the above variable does for
  824.      levels.  */
  825.   bits_list_type group_match_status;
  826.  
  827.   /* Keeps track of whether or not a given group is active.  Accessed as
  828.      is group_match_status.  */
  829.   bits_list_type group_active_status;
  830.  
  831.   /* Keeps track of operations relevant to detecting valid position of '$'.  */
  832.   op_list_type op_list;
  833.  
  834.   /* Keeps track of whether or not hit a `$' since the the beginning of
  835.      the pattern or the last (if any) alternative; if so, then `^' is an
  836.      ordinary character.  */
  837.      
  838.   boolean had_an_endline = false;
  839.  
  840.  
  841.   compile_stack.stack
  842.     = (compile_stack_element *) malloc (INIT_COMPILE_STACK_SIZE
  843.                                         * sizeof (compile_stack_element));
  844.  
  845.   if (compile_stack.stack == NULL)
  846.     goto memory_exhausted;
  847.  
  848.   compile_stack.size = INIT_COMPILE_STACK_SIZE;
  849.   compile_stack.avail = 0;
  850.  
  851.  
  852.   if (syntax & RE_REPEATED_ANCHORS_AWAY)
  853.     if (!init_pattern_offsets_list (&anchor_list, 
  854.         INIT_COMPILE_STACK_SIZE << 1))
  855.       goto memory_exhausted;
  856.  
  857.   if (!(init_bits_list (&level_match_status)  
  858.        && init_bits_list (&group_match_status)
  859.        && init_bits_list (&group_active_status)))
  860.     goto memory_exhausted;
  861.  
  862.  
  863.   if (!init_pattern_offsets_list (&op_list, INIT_PATTERN_OFFSETS_LIST_SIZE))
  864.     goto memory_exhausted;
  865.  
  866.  
  867.   bufp->syntax = syntax;
  868.   bufp->fastmap_accurate = 0;
  869.   bufp->not_bol = bufp->not_eol = 0;
  870.  
  871.   /* Always count groups, whether or not bufp->no_sub is set.  */
  872.   bufp->re_nsub = 0;                
  873.  
  874. #ifndef emacs
  875. #ifndef SYNTAX_TABLE
  876.   /* Initialize the syntax table.  */
  877.    init_syntax_once();
  878. #endif
  879. #endif
  880.  
  881.  
  882.   if (bufp->allocated == 0)
  883.     {
  884.       bufp->allocated = INIT_BUF_SIZE;
  885.       if (bufp->buffer)
  886.     {    
  887.           /* EXTEND_BUFFER loses when bufp->allocated is 0.  This loses if
  888.              buffer's address is bogus.  */
  889.           bufp->buffer = (char *) realloc (bufp->buffer, INIT_BUF_SIZE);
  890.         }
  891.       else
  892.         {
  893.           /* Caller did not allocate a buffer.  Do it for them.  */
  894.           bufp->buffer = (char *) malloc (INIT_BUF_SIZE);
  895.         }
  896.       if (!bufp->buffer) goto memory_exhausted;
  897.       begalt = b = bufp->buffer;
  898.     }
  899.  
  900.   while (p != pend)
  901.     {
  902.       PATFETCH (c);
  903.  
  904.       switch (c)
  905.         {
  906.         case '$':
  907.           {
  908.             if ((syntax & RE_ANCHORS_ONLY_AT_ENDS) && p != pend
  909.                 && (syntax & RE_CONTEXT_INVALID_ANCHORS))
  910.                   goto invalid_pattern;
  911.  
  912.         if (syntax & RE_TIGHT_ALT)
  913.               {
  914.                 /* Make operand of last alternation jump to this endline.  */
  915.  
  916.                 if (fixup_alt_jump)
  917.                   store_jump (fixup_alt_jump, jump_past_next_alt, b);
  918.  
  919.                 fixup_alt_jump = 0;
  920.               }
  921.  
  922.         if (syntax & RE_REPEATED_ANCHORS_AWAY)
  923.               if (!record_anchor_position (!COMPILE_STACK_EMPTY, 
  924.                                            b - bufp->buffer, &anchor_list))
  925.                 goto memory_exhausted;
  926.  
  927.         if (!add_op (&op_list, b - bufp->buffer))
  928.               goto memory_exhausted;
  929.               
  930.             BUF_PUSH ((p != pend && *p == '\n')
  931.                       ? (int) endline_before_newline
  932.               : (int) endline);
  933.             
  934.         /* If there's a chance this endline would have to turn into
  935.            `exactn 1 '$',' have to push dummy ops to make room;
  936.                can't insert later because would mess up any surrounding
  937.                jumps.  */
  938.  
  939.             if (!(syntax & RE_CONTEXT_INDEP_ANCHORS)
  940.         && !((syntax & RE_ANCHORS_ONLY_AT_ENDS) && p == pend))
  941.           {
  942.         laststart = b - 1;
  943.                 BUF_PUSH_2 (no_op, no_op);
  944.             }
  945.  
  946.             had_an_endline = true;                
  947.             break;
  948.           }
  949.  
  950.         case '^':
  951.       /* If change anything in this case, have to change analogous
  952.              code in *endline* (yes, endline---because the routine goes
  953.              backwards through the pattern) case of the routine
  954.              verify_and_adjust_endlines.  */
  955.              
  956.           /* ^ means match the beginning of a string.  If
  957.              RE_CONTEXT_INDEP_ANCHORS is set, then it represents the
  958.              match-beginning-of-line operator anywhere in the regular
  959.              expression.
  960.  
  961.              If that bit isn't set, then it represents the
  962.              match-beginning-of-line operator in leading positions and
  963.              matches itself in other positions (unless it's invalid
  964.              there).  */
  965.  
  966.        /* If the '^' must be at the pattern's beginning or else is
  967.               in a leading position.  */
  968.        
  969.            if (((syntax & RE_ANCHORS_ONLY_AT_ENDS) 
  970.             || (syntax & RE_TIGHT_ALT))
  971.            ? p - 1 == pattern
  972.  
  973.                /* If just after a newline, or...  */
  974.                : ((p - 2 >= pattern && p[-2] == '\n')
  975.  
  976.                /* ...no levels match anything, then in a leading position.  */
  977.  
  978.                   || no_levels_match_anything (level_match_status)))
  979.          {
  980.            if (had_an_endline)
  981.              goto normal_char;
  982.  
  983.                if (syntax & RE_REPEATED_ANCHORS_AWAY)
  984.                  if (!record_anchor_position (!COMPILE_STACK_EMPTY, 
  985.                           b - bufp->buffer, &anchor_list))
  986.                goto memory_exhausted;
  987.  
  988.              }
  989.     
  990.            else if (syntax & RE_CONTEXT_INVALID_ANCHORS)
  991.              goto invalid_pattern;
  992.  
  993.        /* If not just after a newline and not always supposed to be
  994.               an anchor, consider it a ordinary character. */
  995.            
  996.            else if (!(syntax & RE_CONTEXT_INDEP_ANCHORS)
  997.                     && ((syntax & RE_ANCHORS_ONLY_AT_ENDS)
  998.                      /* To make, e.g., `^(^a)' match `^a'.  */
  999.                         ? p - 1 != pattern
  1000.                         : (int)laststart))
  1001.              goto normal_char;
  1002.  
  1003.            if (syntax & RE_TIGHT_ALT)
  1004.              {
  1005.                if (p != pattern + 1 && !(syntax & RE_CONTEXT_INDEP_ANCHORS))
  1006.                  goto normal_char;
  1007.  
  1008.                BUF_PUSH (begline);
  1009.                begalt = b;  /* Make alternative begin after the '^'.  */
  1010.              }
  1011.            else
  1012.              BUF_PUSH (begline);
  1013.  
  1014.            break;
  1015.  
  1016.         case '+':
  1017.         case '?':
  1018.           if ((syntax & RE_BK_PLUS_QM)
  1019.               || (syntax & RE_LIMITED_OPS))
  1020.             goto normal_char;
  1021.         handle_plus:
  1022.         case '*':
  1023.           /* If there is no previous pattern... */
  1024.           if (!laststart)
  1025.             {
  1026.               if (syntax & RE_CONTEXT_INVALID_OPS)
  1027.                 goto missing_preceding_re;
  1028.               else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  1029.                 goto normal_char;
  1030.             }
  1031.  
  1032.           if ((syntax & RE_REPEATED_ANCHORS_AWAY)
  1033.               && (enum regexpcode) *laststart == start_memory)
  1034.             remove_intervening_anchors (laststart, b, anchor_list, bufp);
  1035.  
  1036.           /* If there is a sequence of repetition chars, collapse it
  1037.              down to just one.  We can't combine interval operators with
  1038.              these because we'd incorrect behavior for, e.g., `a{2}*',
  1039.              which should only match an even number of `a's.  */
  1040.              
  1041.           zero_times_ok = 0;
  1042.           many_times_ok = 0;
  1043.  
  1044.           while (1)
  1045.             {
  1046.               zero_times_ok |= c != '+';
  1047.               many_times_ok |= c != '?';
  1048.  
  1049.               if (p == pend)
  1050.                 break;
  1051.  
  1052.               PATFETCH (c);
  1053.  
  1054.               if (c == '*')
  1055.         {
  1056.                   if (syntax & RE_NO_CONSECUTIVE_REPEATS)
  1057.             goto invalid_preceding_re;                    
  1058.                 }
  1059.           else if (!(syntax & RE_BK_PLUS_QM)
  1060.                && (c == '+' || c == '?'))
  1061.         {
  1062.                   if (syntax & RE_NO_CONSECUTIVE_REPEATS)
  1063.             goto invalid_preceding_re;                    
  1064.                 }
  1065.               else if ((syntax & RE_BK_PLUS_QM)
  1066.                && c == '\\')
  1067.         {
  1068.           if (p == pend)
  1069.                     goto trailing_backslash;
  1070.                     
  1071.                   PATFETCH (c1);
  1072.  
  1073.                   if (!(c1 == '+' || c1 == '?'))
  1074.             {
  1075.               PATUNFETCH;
  1076.               PATUNFETCH;
  1077.               break;
  1078.             }
  1079.  
  1080.                   if (syntax & RE_NO_CONSECUTIVE_REPEATS)
  1081.             goto invalid_preceding_re;                    
  1082.  
  1083.                   c = c1;
  1084.         }
  1085.           else
  1086.         {
  1087.           PATUNFETCH;
  1088.           break;
  1089.         }
  1090.          }
  1091.  
  1092.       /* Star, etc. applied to an empty pattern is equivalent
  1093.          to an empty pattern.  */
  1094.      if (!laststart)  
  1095.         break;
  1096.  
  1097.           /* Now we know whether or not zero matches is allowed
  1098.          and also whether or not two or more matches is allowed.  */
  1099.  
  1100.           if (!add_op (&op_list, b - bufp->buffer))
  1101.             goto memory_exhausted;
  1102.  
  1103.           /* If more than one repetition is allowed, put in at the
  1104.              end a backward relative jump from b to before the next jump
  1105.              we're going to put in below (which jumps from laststart to
  1106.              after this jump).  */
  1107.      
  1108.           if (many_times_ok)
  1109.             {
  1110.           GET_BUFFER_SPACE (3);
  1111.               store_jump (b, maybe_pop_jump, laststart - 3);
  1112.               b += 3;      /* Because store_jump puts stuff here.  */
  1113.         }
  1114.           /* Otherwise, put in a no_op so verify_and_adjust_endlines can
  1115.              detect that, e.g., a preceding `$' is not an anchor.  */
  1116.           else
  1117.             BUF_PUSH (no_op);
  1118.             
  1119.  
  1120.           /* On failure, jump from laststart to b + 3, which will be the
  1121.              end of the buffer after this jump is inserted.  */
  1122.  
  1123.           if (syntax & RE_REPEATED_ANCHORS_AWAY)
  1124.             adjust_pattern_offsets_list (3, laststart - bufp->buffer, 
  1125.                      &anchor_list);
  1126.  
  1127.           adjust_pattern_offsets_list (3, laststart - bufp->buffer, &op_list);
  1128.           GET_BUFFER_SPACE (3);
  1129.           insert_jump (on_failure_jump, laststart, b + 3, b);
  1130.           pending_exact = 0;
  1131.       b += 3;
  1132.           
  1133.       if (!zero_times_ok)
  1134.         {
  1135.           /* At least one repetition is required, so insert a
  1136.                  dummy_failure before the initial on_failure_jump
  1137.                  instruction of the loop. This effects a skip over that
  1138.                  instruction the first time we hit that loop.  */
  1139.  
  1140.             if (syntax & RE_REPEATED_ANCHORS_AWAY)
  1141.                 adjust_pattern_offsets_list (3, laststart - bufp->buffer, 
  1142.                                  &anchor_list);
  1143.  
  1144.               adjust_pattern_offsets_list (3, laststart - bufp->buffer, 
  1145.                                &op_list);
  1146.               GET_BUFFER_SPACE (3);
  1147.               insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  1148.           b += 3;
  1149.             }
  1150.       break;
  1151.  
  1152.     case '.':
  1153.       laststart = b;
  1154.  
  1155.           if (!add_op (&op_list, b - bufp->buffer))
  1156.             goto memory_exhausted;
  1157.  
  1158.           BUF_PUSH (anychar);
  1159.  
  1160.           if (!set_this_level (&level_match_status, current_level)
  1161.               || !set_match_status_of_active_groups (group_active_status,
  1162.                              &group_match_status))
  1163.             goto memory_exhausted;
  1164.  
  1165.           break;
  1166.  
  1167.         case '[':
  1168.       {
  1169.             unsigned just_had_a_char_class = 0;
  1170.             
  1171.             if (p == pend)
  1172.               goto unmatched_left_bracket;
  1173.  
  1174.             while (b - bufp->buffer
  1175.                    > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  1176.               EXTEND_BUFFER;
  1177.  
  1178.             laststart = b;
  1179.  
  1180.             if (!add_op (&op_list, b - bufp->buffer))
  1181.               goto memory_exhausted;
  1182.  
  1183.             if (*p == '^')
  1184.               {
  1185.                 BUF_PUSH (charset_not); 
  1186.                 p++;
  1187.               }
  1188.             else
  1189.               BUF_PUSH (charset);
  1190.  
  1191.             /* Remember the first position in the bracket expression.  */
  1192.             p1 = p;
  1193.  
  1194.             BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  1195.             /* Clear the whole map */
  1196.             bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  1197.  
  1198.             if ((syntax & RE_HAT_LISTS_NOT_NEWLINE) 
  1199.                  && (enum regexpcode) b[-2] == charset_not)
  1200.               SET_LIST_BIT ('\n');
  1201.  
  1202.  
  1203.             /* Read in characters and ranges, setting map bits.  */
  1204.             while (1)
  1205.               {
  1206.                 if (p == pend)
  1207.                   goto unmatched_left_bracket;
  1208.  
  1209.         PATFETCH (c);
  1210.  
  1211.  
  1212.                 /* If set, \ escapes characters when inside [...].  */
  1213.                 if ((syntax & RE_AWK_CLASS_HACK) && c == '\\')
  1214.                   {
  1215.             if (p == pend)
  1216.                       goto trailing_backslash;
  1217.                       
  1218.                     PATFETCH(c1);
  1219.                     SET_LIST_BIT (c1);
  1220.                     continue;
  1221.                   }
  1222.                 /* Could be the end of the bracket expression.  If it's
  1223.                    not (i.e., when the bracket expression is `[]' so
  1224.                    far), the ']' character bit gets set way below.  */
  1225.  
  1226.                 if (c == ']' && p != p1 + 1)
  1227.           break;
  1228.  
  1229.  
  1230.                 /* Look ahead to see if it's a range when the last thing
  1231.                    was a character class.  */
  1232.  
  1233.                 if (just_had_a_char_class && c == '-' && *p != ']')
  1234.                   goto invalid_range_end;
  1235.             
  1236.            /* Look ahead to see if it's a range when the last thing
  1237.                    was a character: if this is a hyphen not at the
  1238.                    beginning or the end of a list, then it's the range
  1239.                    operator.  */
  1240.                    
  1241.         if (c == '-' 
  1242.                     && !(p - 2 >= pattern && p[-2] == '[') 
  1243.                 && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  1244.                     && *p != ']')
  1245.                   {
  1246.                     DO_RANGE;
  1247.                   }
  1248.                   
  1249.                 else if (p[0] == '-' && p[1] != ']')
  1250.                   {
  1251.                     /* This handles ranges made up of characters only.  */
  1252.                     PATFETCH (c1);        /* The `-'.  */
  1253.             DO_RANGE;
  1254.                   }
  1255.  
  1256.                 /* See if we're at the beginning of a possible character
  1257.                    class.  */
  1258.    
  1259.                 else if ((syntax & RE_CHAR_CLASSES)
  1260.                           &&  c == '[' && p[0] == ':')
  1261.                   {
  1262.                     /* Longest valid character class word has six chars.  */
  1263.                     char str[CHAR_CLASS_MAX_LENGTH];
  1264.                     
  1265.                     PATFETCH (c);
  1266.                     c1 = 0;
  1267.  
  1268.                     /* If pattern is `[[:'.  */
  1269.                     if (p == pend)
  1270.                       goto unmatched_left_bracket;
  1271.  
  1272.                     while (1)
  1273.                       {
  1274.                         /* Don't translate the ``character class''
  1275.                            characters.  */
  1276.                         PATFETCH_RAW (c);
  1277.                         if (c == ':' || c == ']' || p == pend
  1278.                             || c1 == CHAR_CLASS_MAX_LENGTH)
  1279.                           break;
  1280.                         str[c1++] = c;
  1281.                       }
  1282.                     str[c1] = '\0';
  1283.                     
  1284.                     /* If isn't a word bracketed by `[:' and:`]':
  1285.                        undo the ending character, the letters, and leave 
  1286.                        the leading `:' and `[' (but set bits for them).  */
  1287.  
  1288.                     if (c == ':' && p[0] == ']')
  1289.                       {
  1290.                         if (!IS_CHAR_CLASS (str))
  1291.                           goto invalid_char_class;
  1292.                           
  1293.                         /* The ] at the end of the character class.  */
  1294.                         PATFETCH (c);                    
  1295.  
  1296.                         if (p == pend)
  1297.                           goto unmatched_left_bracket;
  1298.                           
  1299.                         for (c = 0; c < (1 << BYTEWIDTH); c++)
  1300.                           {
  1301.                             if ((strcmp (str, "alpha") == 0  && isalpha (c))
  1302.                               || (strcmp (str, "upper") == 0  && isupper (c))
  1303.                               || (strcmp (str, "lower") == 0  && islower (c))
  1304.                               || (strcmp (str, "digit") == 0  && isdigit (c))
  1305.                               || (strcmp (str, "alnum") == 0  && isalnum (c))
  1306.                               || (strcmp (str, "xdigit") == 0  && isxdigit (c))
  1307.                               || (strcmp (str, "space") == 0  && isspace (c))
  1308.                               || (strcmp (str, "print") == 0  && isprint (c))
  1309.                               || (strcmp (str, "punct") == 0  && ispunct (c))
  1310.                               || (strcmp (str, "graph") == 0  && isgraph (c))
  1311.                               || (strcmp (str, "cntrl") == 0  && iscntrl (c)))
  1312.                             SET_LIST_BIT (c);
  1313.                           }
  1314.                         just_had_a_char_class = 1;
  1315.                       }
  1316.                     else
  1317.               {
  1318.                         c1++;
  1319.                         while (c1--)    
  1320.                           PATUNFETCH;
  1321.                         SET_LIST_BIT ('[');
  1322.                         SET_LIST_BIT (':');
  1323.                         just_had_a_char_class = 0;
  1324.                       }
  1325.                   }
  1326.                 else
  1327.                   {
  1328.                     just_had_a_char_class = 0;
  1329.                     SET_LIST_BIT (c);
  1330.                   }
  1331.               }
  1332.  
  1333.             /* Discard any (non)matching list bytes that are all 0 at the
  1334.                end of the map. Decrement the map-length byte too.  */
  1335.  
  1336.             while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  1337.               b[-1]--; 
  1338.             b += b[-1];
  1339.           }
  1340.  
  1341.           if (!set_this_level (&level_match_status, current_level)
  1342.               || !set_match_status_of_active_groups (group_active_status,
  1343.                              &group_match_status))
  1344.             goto memory_exhausted;
  1345.  
  1346.           break;
  1347.  
  1348.  
  1349.         case '(':
  1350.       if (!(syntax & RE_NO_BK_PARENS))
  1351.         goto normal_char;
  1352.       else
  1353.         goto handle_open;
  1354.  
  1355.     case ')':
  1356.       if (! (syntax & RE_NO_BK_PARENS))
  1357.         goto normal_char;
  1358.       else
  1359.         goto handle_close;
  1360.  
  1361.         case '\n':
  1362.       if (! (syntax & RE_NEWLINE_ALT))
  1363.         goto normal_char;
  1364.       else
  1365.         goto handle_bar;
  1366.  
  1367.     case '|':
  1368.           if (!(syntax & RE_NO_BK_VBAR))
  1369.         goto normal_char;
  1370.       else
  1371.         goto handle_bar;
  1372.  
  1373.     case '{':
  1374.            if ((syntax & RE_NO_BK_BRACES)
  1375.                 && (syntax & RE_INTERVALS))
  1376.              goto handle_interval;
  1377.            else
  1378.              goto normal_char;
  1379.              
  1380.         case '\\':
  1381.       if (p == pend) 
  1382.             goto trailing_backslash;
  1383.     
  1384.           PATFETCH_RAW (c);
  1385.       switch (c)
  1386.         {
  1387.             case '(':
  1388.           if (syntax & RE_NO_BK_PARENS)
  1389.         goto normal_backsl;
  1390.             handle_open:
  1391.               bufp->re_nsub++;
  1392.               increase_level (¤t_level);
  1393.  
  1394.               if (!make_group_active (&group_active_status, regnum))
  1395.             goto memory_exhausted;
  1396.         
  1397.               if (syntax & RE_NO_EMPTY_GROUPS)
  1398.                 {
  1399.                   p1 = p;
  1400.                   if (*p1 == '^') p1++;
  1401.                   if (*p1 == '$') p1++;
  1402.           if (!(syntax & RE_NO_BK_PARENS) && *p1 == '\\') p1++;
  1403.                   
  1404.           /* If found an empty group...  */
  1405.                   if (*p1 == ')') 
  1406.                     goto invalid_pattern;
  1407.                 }
  1408.             
  1409.               /* Value to restore in laststart when hit end of this
  1410.                  group; should point to the start_memory that we are
  1411.                  about to push.  */
  1412.                  
  1413.           if (COMPILE_STACK_FULL)
  1414.                 { 
  1415.                   compile_stack.stack = (compile_stack_element *) 
  1416.                     realloc (compile_stack.stack,
  1417.                              (compile_stack.size << 1)
  1418.                              * sizeof (compile_stack_element));
  1419.             
  1420.                   if (compile_stack.stack == NULL)
  1421.             goto memory_exhausted;
  1422.  
  1423.                   compile_stack.size <<= 1;
  1424.           }
  1425.  
  1426.               compile_stack.stack[compile_stack.avail].laststart_offset 
  1427.                 = b - bufp->buffer;
  1428.           compile_stack.stack[compile_stack.avail].fixup_alt_jump 
  1429.                 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
  1430.           compile_stack.stack[compile_stack.avail].regnum = regnum;
  1431.           compile_stack.stack[compile_stack.avail].begalt_offset 
  1432.                 = begalt - bufp->buffer;
  1433.             compile_stack.avail++;
  1434.  
  1435.           if (!add_op (&op_list, b - bufp->buffer))
  1436.                 goto memory_exhausted;
  1437.  
  1438.               BUF_PUSH_2 (start_memory, regnum);
  1439.               regnum++;
  1440.               fixup_alt_jump = 0;
  1441.           laststart = 0;
  1442.           begalt = b;
  1443.               break;
  1444.  
  1445.         case ')':
  1446.           if (syntax & RE_NO_BK_PARENS)
  1447.         goto normal_backsl;
  1448.           
  1449.           if (COMPILE_STACK_EMPTY)
  1450.         if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  1451.                   goto normal_backsl;
  1452.                 else
  1453.                   goto unmatched_close;    
  1454.         
  1455.             handle_close:
  1456.               if (fixup_alt_jump)
  1457.         store_jump (fixup_alt_jump, jump_past_next_alt, b);
  1458.           
  1459.               /* See similar code for backslashed parens above.  */
  1460.               
  1461.               if (COMPILE_STACK_EMPTY)
  1462.         if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  1463.                   goto normal_char;
  1464.                 else
  1465.                   goto unmatched_close;    
  1466.  
  1467.           if (get_level_match_status (level_match_status, current_level))
  1468.                 if (!set_next_lower_level (&level_match_status, current_level))
  1469.           goto memory_exhausted;
  1470.                
  1471.               /* Only call these if know you have a matched close.  */
  1472.               decrease_level (¤t_level);
  1473.           make_group_inactive (&group_active_status, regnum);
  1474.  
  1475.               compile_stack.avail--;        
  1476.           begalt 
  1477.                 = compile_stack.stack[compile_stack.avail].begalt_offset 
  1478.                 + bufp->buffer;
  1479.               laststart 
  1480.                 = (compile_stack.stack[compile_stack.avail].laststart_offset
  1481.                   + bufp->buffer);
  1482.  
  1483.               fixup_alt_jump = compile_stack.stack[compile_stack.avail].fixup_alt_jump
  1484.                          ? compile_stack.stack[compile_stack.avail]
  1485.                              .fixup_alt_jump + bufp->buffer - 1 
  1486.                            : 0;
  1487.  
  1488.               if (!add_op (&op_list, b - bufp->buffer))
  1489.                 goto memory_exhausted;
  1490.  
  1491.               BUF_PUSH_2 (stop_memory, 
  1492.                       compile_stack.stack[compile_stack.avail].regnum);
  1493.           break;
  1494.  
  1495.         case '|':                    /* `\|'.  */
  1496.               if ((syntax & RE_LIMITED_OPS)
  1497.               || (syntax & RE_NO_BK_VBAR))
  1498.         goto normal_backsl;
  1499.         handle_bar:
  1500.               if (syntax & RE_LIMITED_OPS)
  1501.                 goto normal_char;
  1502.                
  1503.               /* Disallow empty alternatives if RE_NO_EMPTY_ALTS is set.
  1504.                  Caveat: can't detect if the vbar is followed by a
  1505.                  trailing '$' yet, unless it's the last thing in a
  1506.                  pattern; the routine for verifying endlines has to do
  1507.                  the rest.  */
  1508.  
  1509.               if ((syntax & RE_NO_EMPTY_ALTS)
  1510.                   && (!laststart  ||  p == pend 
  1511.                       || (*p == '$' && p + 1 == pend)
  1512.                       || ((syntax & RE_NO_BK_PARENS)
  1513.                            ? (p < pend  &&  *p == ')')
  1514.                            : (p + 1 < pend && p[0] == '\\' && p[1] == ')'))))
  1515.                 goto invalid_pattern;
  1516.  
  1517.           
  1518.               /* Clear some variables.  */
  1519.               
  1520.               if (lower_levels_match_nothing (level_match_status, 
  1521.                               current_level))
  1522.             clear_this_and_higher_levels (&level_match_status, 
  1523.                               current_level);
  1524.           had_an_endline = false;
  1525.               
  1526.  
  1527.               /* Insert before the previous alternative a jump which
  1528.                  jumps to this alternative if the former fails.  */
  1529.           
  1530.               if (syntax & RE_REPEATED_ANCHORS_AWAY)
  1531.             adjust_pattern_offsets_list (3, begalt - bufp->buffer, 
  1532.                                  &anchor_list);
  1533.           
  1534.               adjust_pattern_offsets_list (3, begalt - bufp->buffer, &op_list);
  1535.               GET_BUFFER_SPACE (3);
  1536.               insert_jump (on_failure_jump, begalt, b + 6, b);
  1537.           pending_exact = 0;
  1538.           b += 3;
  1539.  
  1540.               /* The alternative before this one has a jump after it
  1541.                  which gets executed if it gets matched.  Adjust that
  1542.                  jump so it will jump to this alternative's analogous
  1543.                  jump (put in below, which in turn will jump to the next
  1544.                  (if any) alternative's such jump, etc.).  The last such
  1545.                  jump jumps to the correct final destination.  A picture:
  1546.                       _____ _____ 
  1547.                       |   | |   |   
  1548.               |   v |   v 
  1549.                      a | b   | c   
  1550.          
  1551.                  If we are at `b,' then fixup_alt_jump right now points to a
  1552.                  three-byte space after `a.'  We'll put in the jump, set
  1553.                  fixup_alt_jump to right after `b,' and leave behind three
  1554.                  bytes which we'll fill in when we get to after `c.'  */
  1555.  
  1556.               if (fixup_alt_jump)
  1557.         store_jump (fixup_alt_jump, jump_past_next_alt, b);
  1558.                 
  1559.           /* Mark and leave space for a jump after this alternative
  1560.                  ---to be filled in later either by next alternative or
  1561.                  when know we're at the end of a series of alternatives.  */
  1562.  
  1563.           if (!add_op (&op_list, b - bufp->buffer))
  1564.                 goto memory_exhausted;
  1565.  
  1566.               fixup_alt_jump = b;
  1567.           GET_BUFFER_SPACE (3);
  1568.               b += 3;
  1569.  
  1570.               laststart = 0;
  1571.           begalt = b;
  1572.           break;
  1573.  
  1574.             case '{': 
  1575.                   /* If \{ is a literal.  */
  1576.               if (!(syntax & RE_INTERVALS)
  1577.           /* If we're at a "\{" and it's not the open-interval 
  1578.                      operator.  */
  1579.                   || ((syntax & RE_INTERVALS)
  1580.                       && (syntax & RE_NO_BK_BRACES))
  1581.                   || (p - 2 == pattern  &&  p == pend))
  1582.                 goto normal_backsl;
  1583.  
  1584.             handle_interval:
  1585.           /* If got here, then intervals must be allowed.  */
  1586.  
  1587.               beg_interval = p - 1;         /* The `{'.  */
  1588.           following_left_brace = 0;
  1589.               lower_bound = -1;            /* So can see if are set.  */
  1590.           upper_bound = -1;
  1591.               
  1592.               if (p == pend)
  1593.         {
  1594.                   if (syntax & RE_NO_BK_BRACES)
  1595.             goto unfetch_interval;
  1596.                   else
  1597.                     goto unmatched_left_curly_brace;
  1598.                 }
  1599.  
  1600.               GET_UNSIGNED_NUMBER (lower_bound);
  1601.  
  1602.               if (c == ',')
  1603.         {
  1604.                   GET_UNSIGNED_NUMBER (upper_bound);
  1605.           if (upper_bound < 0)
  1606.             upper_bound = RE_DUP_MAX;
  1607.         }
  1608.                 
  1609.           if (upper_bound < 0)
  1610.         upper_bound = lower_bound;
  1611.  
  1612.           if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  1613.           || lower_bound > upper_bound)
  1614.         {
  1615.                   if (syntax & RE_NO_BK_BRACES)
  1616.                     goto unfetch_interval;
  1617.                   else 
  1618.                     goto invalid_braces_content;
  1619.                 }
  1620.  
  1621.               if (!(syntax & RE_NO_BK_BRACES)) 
  1622.                 {
  1623.                   if (c != '\\')
  1624.                     goto unmatched_left_curly_brace;
  1625.                     
  1626.                   PATFETCH (c);
  1627.                 }
  1628.  
  1629.               if (c != '}')
  1630.         {
  1631.                   if (syntax & RE_NO_BK_BRACES)
  1632.                     goto unfetch_interval;
  1633.                   else 
  1634.                     goto invalid_braces_content;
  1635.                 }
  1636.  
  1637.  
  1638.           /* Parsed a valid interval, but if an interval can't
  1639.                  operate on another repetition operator, check that what
  1640.                  follows isn't one.  */
  1641.  
  1642.               if ((syntax & RE_NO_CONSECUTIVE_REPEATS) && p != pend)
  1643.                 {
  1644.           if (*p == '*' || *p == '+' || *p == '?')
  1645.                     goto invalid_preceding_re;
  1646.                     
  1647.                   if (syntax & RE_NO_BK_BRACES)
  1648.             {
  1649.                       if (*p == '{')
  1650.                         {
  1651.                           /* Close but not exactly as above.  */
  1652.                           
  1653.                           int lower_bound = -1;
  1654.                           int upper_bound = -1;
  1655.  
  1656.                           following_left_brace = p++;
  1657.                           GET_UNSIGNED_NUMBER (lower_bound);
  1658.  
  1659.                           if (c == ',')
  1660.                             {
  1661.                               GET_UNSIGNED_NUMBER (upper_bound);
  1662.                               if (upper_bound < 0)
  1663.                                 upper_bound = RE_DUP_MAX;
  1664.                             }
  1665.  
  1666.                           if (upper_bound < 0)
  1667.                             upper_bound = lower_bound;
  1668.  
  1669.               /* If not a valid interval, then we don't have
  1670.                              an interval operating on another one; what
  1671.                              we have instead is a series match-self ops
  1672.                              starting with a '{'.  */
  1673.                           
  1674.                           if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  1675.                               || lower_bound > upper_bound || c != '}')
  1676.                 {
  1677.                               /* Back up to  '{' so can use again
  1678.                                  put it in C, as the normal_char label
  1679.                                  code expects that; will go to that
  1680.                                  label after putting the preceding valid
  1681.                                  interval in the buffer.  */
  1682.                                  
  1683.                               p = following_left_brace;
  1684.                               PATFETCH (c);    
  1685.                             }
  1686.                           else
  1687.                             goto invalid_preceding_re;
  1688.                         }
  1689.                     }
  1690.                   else if (p[0] == '\\' && p[1] == '{')
  1691.             goto invalid_preceding_re;
  1692.                 }
  1693.                 
  1694.                       
  1695.           /* We just parsed a valid interval.  */
  1696.               
  1697.               /* If it's invalid to have no preceding re.  */
  1698.               if (!laststart)
  1699.             {
  1700.                   if (syntax & RE_CONTEXT_INVALID_OPS)
  1701.                     goto missing_preceding_re;
  1702.                   else if (syntax & RE_CONTEXT_INDEP_OPS)
  1703.                     laststart = b;
  1704.                   else
  1705.                     goto unfetch_interval;
  1706.                 }
  1707.           else if ((syntax & RE_REPEATED_ANCHORS_AWAY)
  1708.                && (enum regexpcode) *laststart == start_memory)
  1709.         remove_intervening_anchors (laststart, b, anchor_list, bufp);
  1710.                 
  1711.               /* If upper_bound is zero, don't want to succeed at all; 
  1712.           jump from laststart to b + 3, which will be the end of
  1713.                  the buffer after this jump is inserted.  */
  1714.                  
  1715.                if (upper_bound == 0)
  1716.                  {
  1717.                    if (syntax & RE_REPEATED_ANCHORS_AWAY)
  1718.              adjust_pattern_offsets_list (3, laststart - bufp->buffer, 
  1719.                                          &anchor_list);
  1720.  
  1721.                    adjust_pattern_offsets_list (3, laststart - bufp->buffer, 
  1722.                                 &op_list);
  1723.                    GET_BUFFER_SPACE (3);
  1724.                    insert_jump (no_pop_jump, laststart, b + 3, b);
  1725.                    b += 3;
  1726.                  }
  1727.  
  1728.                /* Otherwise, after lower_bound number of succeeds, jump
  1729.                   to after the no_pop_jump_n which will be inserted at the end
  1730.                   of the buffer, and insert that no_pop_jump_n.  */
  1731.                else 
  1732.          { /* Set to 5 if only one repetition is allowed and
  1733.                       hence no no_pop_jump_n is inserted at the current
  1734.                       end of the buffer.  Otherwise, need 10 bytes total
  1735.                       for the succeed_n and the no_pop_jump_n.  */
  1736.                       
  1737.                    unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  1738.                      
  1739.                    GET_BUFFER_SPACE (slots_needed);
  1740.                    /* Initialize the succeed_n to n, even though it will
  1741.                       be set by its attendant set_number_at, because
  1742.                       re_compile_fastmap will need to know it.  Jump to
  1743.                       what the end of buffer will be after inserting
  1744.                       this succeed_n and possibly appending a
  1745.                       no_pop_jump_n.  */
  1746.  
  1747.                if (syntax & RE_REPEATED_ANCHORS_AWAY)
  1748.                      adjust_pattern_offsets_list (5, laststart - bufp->buffer, 
  1749.                                          &anchor_list);
  1750.                   
  1751.                adjust_pattern_offsets_list (5, laststart - bufp->buffer, 
  1752.                                 &op_list);
  1753.                    insert_jump_n (succeed_n, laststart, b + slots_needed, 
  1754.                           b, lower_bound);
  1755.                    b += 5;     /* Just increment for the succeed_n here.  */
  1756.  
  1757.         
  1758.                   /* More than one repetition is allowed, so put in at
  1759.              the end of the buffer a backward jump from b to the
  1760.                      succeed_n we put in above.  By the time we've gotten
  1761.                      to this jump when matching, we'll have matched once
  1762.                      already, so jump back only upper_bound - 1 times.  */
  1763.  
  1764.            if (!add_op (&op_list, b - bufp->buffer))
  1765.                  goto memory_exhausted;
  1766.  
  1767.                    if (upper_bound > 1)
  1768.                      {
  1769.                        store_jump_n (b, no_pop_jump_n, laststart, 
  1770.                              upper_bound - 1);
  1771.                        b += 5;
  1772.                        /* When hit this when matching, reset the
  1773.                           preceding no_pop_jump_n's n to upper_bound - 1.  */
  1774.                           
  1775.                        BUF_PUSH (set_number_at);
  1776.  
  1777.                        /* Only need to get space for the numbers.  */
  1778.                    GET_BUFFER_SPACE (4);
  1779.                        STORE_NUMBER_AND_INCR (b, -5);
  1780.                        STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  1781.                      }
  1782.                    /* Otherwise, put in a no_op, so verify_and_adjust_endlines
  1783.                       can detect, e.g., a preceding `$' is not an anchor.  */
  1784.            else
  1785.              BUF_PUSH (no_op);
  1786.                      
  1787.                     
  1788.                    /* When hit this when matching, set the succeed_n's n.  */
  1789.  
  1790.                    if (syntax & RE_REPEATED_ANCHORS_AWAY)
  1791.              adjust_pattern_offsets_list (5, laststart - bufp->buffer, 
  1792.                                      &anchor_list);
  1793.     
  1794.                    adjust_pattern_offsets_list (5, laststart - bufp->buffer, 
  1795.                                     &op_list);
  1796.                    GET_BUFFER_SPACE (5);
  1797.            insert_op_2 (set_number_at, laststart, b, 5, lower_bound);
  1798.                    b += 5;
  1799.                  }
  1800.               pending_exact = 0;
  1801.           beg_interval = 0;
  1802.           
  1803.               if (following_left_brace)
  1804.         goto normal_char;        
  1805.  
  1806.               break;
  1807.  
  1808.             unfetch_interval:
  1809.           /* If an invalid interval, match the characters as literals.  */
  1810.            if (beg_interval)
  1811.                  p = beg_interval;
  1812.              else
  1813.                  {
  1814.                    fprintf (stderr, 
  1815.               "regex: no interval beginning to which to backtrack.\n");
  1816.            exit (1);
  1817.                  }
  1818.                beg_interval = 0;
  1819.            
  1820.                /* normal_char and normal_backsl expect a character in `c'.  */
  1821.                PATFETCH (c);    
  1822.  
  1823.                if (!(syntax & RE_NO_BK_BRACES))
  1824.                  {
  1825.                    if (p > pattern  &&  p[-1] == '\\')
  1826.              goto normal_backsl;
  1827.                  }
  1828.                goto normal_char;
  1829.  
  1830. #ifdef emacs
  1831.         case '=':
  1832.           BUF_PUSH (at_dot);
  1833.           break;
  1834.  
  1835.         case 's':    
  1836.           laststart = b;
  1837.           PATFETCH (c);
  1838.           BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
  1839.           break;
  1840.  
  1841.         case 'S':
  1842.           laststart = b;
  1843.           PATFETCH (c);
  1844.           BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
  1845.           break;
  1846. #endif /* emacs */
  1847.  
  1848.         case 'w':
  1849.           laststart = b;
  1850.  
  1851.               if (!add_op (&op_list, b - bufp->buffer))
  1852.                 goto memory_exhausted;
  1853.  
  1854.           BUF_PUSH (wordchar);
  1855.  
  1856.               if (!set_this_level (&level_match_status, current_level)
  1857.                   || !set_match_status_of_active_groups (group_active_status,
  1858.                              &group_match_status))
  1859.                 goto memory_exhausted;
  1860.  
  1861.               break;
  1862.  
  1863.         case 'W':
  1864.           laststart = b;
  1865.  
  1866.               if (!add_op (&op_list, b - bufp->buffer))
  1867.                 goto memory_exhausted;
  1868.  
  1869.           BUF_PUSH (notwordchar);
  1870.     
  1871.               if (!set_this_level (&level_match_status, current_level)
  1872.                   || !set_match_status_of_active_groups (group_active_status,
  1873.                              &group_match_status))
  1874.                 goto memory_exhausted;
  1875.  
  1876.               break;
  1877.  
  1878.         case '<':
  1879.           BUF_PUSH (wordbeg);
  1880.           break;
  1881.  
  1882.         case '>':
  1883.           BUF_PUSH (wordend);
  1884.           break;
  1885.  
  1886.         case 'b':
  1887.           BUF_PUSH (wordbound);
  1888.           break;
  1889.  
  1890.         case 'B':
  1891.           BUF_PUSH (notwordbound);
  1892.           break;
  1893.  
  1894.         case '`':
  1895.           BUF_PUSH (begbuf);
  1896.           break;
  1897.  
  1898.         case '\'':
  1899.           BUF_PUSH (endbuf);
  1900.           break;
  1901.  
  1902.         case '1':
  1903.         case '2':
  1904.         case '3':
  1905.         case '4':
  1906.         case '5':
  1907.         case '6':
  1908.         case '7':
  1909.         case '8':
  1910.         case '9':
  1911.           if (syntax & RE_NO_BK_REFS)
  1912.                 goto normal_char;
  1913.  
  1914.               c1 = c - '0';
  1915.  
  1916.               if (c1 >= regnum)
  1917.         {
  1918.             if (syntax & RE_NO_MISSING_BK_REF)
  1919.                     goto invalid_back_reference;
  1920.                   else
  1921.                     goto normal_char;
  1922.                 }
  1923.  
  1924.               /* Can't back reference to a subexpression if inside of it.  */
  1925.           if (is_in_compile_stack (compile_stack, c1))
  1926.                 goto normal_char;
  1927.  
  1928.               laststart = b;
  1929.  
  1930.               if (!add_op (&op_list, b - bufp->buffer))
  1931.                 goto memory_exhausted;
  1932.  
  1933.           BUF_PUSH_2 (duplicate, c1);
  1934.               
  1935.               if (get_group_match_status (group_match_status, c1))
  1936.         if (!set_this_level (&level_match_status, current_level))
  1937.                   goto memory_exhausted;
  1938.  
  1939.               break;
  1940.  
  1941.         case '+':
  1942.         case '?':
  1943.           if (syntax & RE_BK_PLUS_QM)
  1944.         goto handle_plus;
  1945.           else
  1946.                 goto normal_backsl;
  1947.               break;
  1948.  
  1949.             default:
  1950.         normal_backsl:
  1951.           /* You might think it would be useful for \ to mean
  1952.          not to translate; but if we don't translate it
  1953.          it will never match anything.  */
  1954.     
  1955.               if (translate) 
  1956.                 c = translate[c];
  1957.     
  1958.               goto normal_char;
  1959.         }
  1960.       break;
  1961.  
  1962.         default:
  1963.  
  1964.         /* Expects the character in `c'!  */
  1965.     normal_char:
  1966.           /* If no exactn currently being built.  */
  1967.           if (!pending_exact 
  1968.  
  1969.               /* If last exactn not at current position.  */
  1970.               || pending_exact + *pending_exact + 1 != b
  1971.               
  1972.           || *pending_exact == 0177 
  1973.  
  1974.               /* If followed by a repetition operator.  */
  1975.               || *p == '*' || *p == '^'
  1976.           || ((syntax & RE_BK_PLUS_QM)
  1977.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  1978.           : (*p == '+' || *p == '?'))
  1979.           || ((syntax & RE_INTERVALS)
  1980.                   && ((syntax & RE_NO_BK_BRACES)
  1981.               ? *p == '{'
  1982.                       : (p[0] == '\\' && p[1] == '{'))))
  1983.         {
  1984.           /* Start building a new exactn.  */
  1985.               
  1986.               laststart = b;
  1987.  
  1988.               if (!add_op (&op_list, b - bufp->buffer))
  1989.                 goto memory_exhausted;
  1990.  
  1991.           BUF_PUSH_2 (exactn, 0);
  1992.           pending_exact = b - 1;
  1993.     
  1994.               if (!set_this_level (&level_match_status, current_level))
  1995.                 goto memory_exhausted;
  1996.             }
  1997.       BUF_PUSH (c);
  1998.           (*pending_exact)++;
  1999.       break;
  2000.           
  2001.         } /* end switch (c).  */
  2002.     } /* end while p!= pend.  */
  2003.  
  2004.   
  2005.   /* Through the pattern now.  */
  2006.   
  2007.   if (fixup_alt_jump)
  2008.     store_jump (fixup_alt_jump, jump_past_next_alt, b);
  2009.  
  2010.   if (!COMPILE_STACK_EMPTY) 
  2011.     goto unmatched_open;
  2012.  
  2013.   /* Have to set this before calling the next routine.  */
  2014.   bufp->used = b - bufp->buffer;
  2015.  
  2016.   if (!verify_and_adjust_endlines (op_list, group_match_status, bufp, 
  2017.                    &enough_memory))
  2018.     goto invalid_pattern;
  2019.   
  2020.   if (!enough_memory)
  2021.     goto memory_exhausted;
  2022.     
  2023.  
  2024.   /* Normal return.  */
  2025.   return 0;
  2026.  
  2027.  
  2028.  /* Abnormal return.  */
  2029.  
  2030.  invalid_pattern:
  2031.    bufp->used = b - bufp->buffer;
  2032.    return "Invalid regular expression";
  2033.  
  2034.  unmatched_open:
  2035.    bufp->used = b - bufp->buffer;
  2036.    return "Unmatched ( or \\(";
  2037.  
  2038.  unmatched_close:
  2039.    bufp->used = b - bufp->buffer;
  2040.    return "Unmatched ) or \\)";
  2041.  
  2042.  end_of_pattern:
  2043.    bufp->used = b - bufp->buffer;
  2044.    return "Premature end of regular expression";
  2045.  
  2046.  too_big:
  2047.    bufp->used = b - bufp->buffer;
  2048.    return "Regular expression too big";
  2049.  
  2050.  memory_exhausted:
  2051.    bufp->used = b - bufp->buffer;
  2052.    return "Memory exhausted";
  2053.   
  2054.  invalid_char_class:
  2055.    bufp->used = b - bufp->buffer;
  2056.    return "Invalid character class name";
  2057.  
  2058.  unmatched_left_bracket:
  2059.    bufp->used = b - bufp->buffer;
  2060.    return "Unmatched [ or [^";
  2061.  
  2062.  invalid_range_end:
  2063.    bufp->used = b - bufp->buffer;
  2064.    return "Invalid range end";
  2065.  
  2066.  trailing_backslash:
  2067.    bufp->used = b - bufp->buffer;
  2068.    return "Trailing backslash";
  2069.    
  2070.  unmatched_left_curly_brace:
  2071.    bufp->used = b - bufp->buffer;
  2072.    return "Unmatched \\{";
  2073.  
  2074.  invalid_braces_content:
  2075.    bufp->used = b - bufp->buffer;
  2076.    return "Invalid content of \\{\\}";
  2077.  
  2078.  missing_preceding_re:
  2079.    bufp->used = b - bufp->buffer;
  2080.    return "Missing preceding regular expression";
  2081.  
  2082.  invalid_preceding_re:
  2083.    bufp->used = b - bufp->buffer;
  2084.    return "Invalid preceding regular expression";
  2085.    
  2086.  invalid_back_reference:
  2087.    bufp->used = b - bufp->buffer;
  2088.    return "Invalid back reference";
  2089. }
  2090.  
  2091.  
  2092. /* Store a jump of the form <OPCODE> <relative address>.
  2093.    Store in the location FROM a jump operation to jump to relative
  2094.    address FROM - TO.  OPCODE is the opcode to store.  */
  2095.  
  2096. static void
  2097. store_jump (from, opcode, to)
  2098.      char *from, *to;
  2099.      char opcode;
  2100. {
  2101.   from[0] = opcode;
  2102.   STORE_NUMBER(from + 1, to - (from + 3));
  2103. }
  2104.  
  2105.  
  2106. /* Open up space before char FROM, and insert there a jump to TO.
  2107.    CURRENT_END gives the end of the storage not in use, so we know 
  2108.    how much data to copy up. OP is the opcode of the jump to insert.
  2109.  
  2110.    If you call this function, you must zero out pending_exact.  */
  2111.  
  2112. static void
  2113. insert_jump (op, from, to, current_end)
  2114.      char op;
  2115.      char *from, *to, *current_end;
  2116. {
  2117.   register char *pfrom = current_end;        /* Copy from here...  */
  2118.   register char *pto = current_end + 3;        /* ...to here.  */
  2119.  
  2120.   while (pfrom != from)                   
  2121.     *--pto = *--pfrom;
  2122.   store_jump (from, op, to);
  2123. }
  2124.  
  2125.  
  2126. /* Store a jump of the form <opcode> <relative address> <n> .
  2127.  
  2128.    Store in the location FROM a jump operation to jump to relative
  2129.    address FROM - TO.  OPCODE is the opcode to store, N is a number the
  2130.    jump uses, say, to decide how many times to jump.
  2131.    
  2132.    If you call this function, you must zero out pending_exact.  */
  2133.  
  2134. static void
  2135. store_jump_n (from, opcode, to, n)
  2136.      char *from, *to;
  2137.      char opcode;
  2138.      unsigned n;
  2139. {
  2140.   from[0] = opcode;
  2141.   STORE_NUMBER (from + 1, to - (from + 3));
  2142.   STORE_NUMBER (from + 3, n);
  2143. }
  2144.  
  2145.  
  2146. /* Similar to insert_jump, but handles a jump which needs an extra
  2147.    number to handle minimum and maximum cases.  Open up space at
  2148.    location FROM, and insert there a jump to TO.  CURRENT_END gives the
  2149.    end of the storage in use, so we know how much data to copy up. OP is
  2150.    the opcode of the jump to insert.
  2151.  
  2152.    If you call this function, you must zero out pending_exact.  */
  2153.  
  2154. static void
  2155. insert_jump_n (op, from, to, current_end, n)
  2156.      char op;
  2157.      char *from, *to, *current_end;
  2158.      unsigned n;
  2159. {
  2160.   register char *pfrom = current_end;        /* Copy from here...  */
  2161.   register char *pto = current_end + 5;        /* ...to here.  */
  2162.  
  2163.   while (pfrom != from)                   
  2164.     *--pto = *--pfrom;
  2165.   store_jump_n (from, op, to, n);
  2166. }
  2167.  
  2168.  
  2169. /* Open up space at location THERE, and insert operation OP followed by
  2170.    NUM_1 and NUM_2.  CURRENT_END gives the end of the storage in use, so
  2171.    we know how much data to copy up.
  2172.  
  2173.    If you call this function, you must zero out pending_exact.  */
  2174.  
  2175. static void
  2176. insert_op_2 (op, there, current_end, num_1, num_2)
  2177.      char op;
  2178.      char *there, *current_end;
  2179.      int num_1, num_2;
  2180. {
  2181.   register char *pfrom = current_end;        /* Copy from here...  */
  2182.   register char *pto = current_end + 5;        /* ...to here.  */
  2183.  
  2184.   while (pfrom != there)                   
  2185.     *--pto = *--pfrom;
  2186.   
  2187.   there[0] = op;
  2188.   STORE_NUMBER (there + 1, num_1);
  2189.   STORE_NUMBER (there + 3, num_2);
  2190. }
  2191.  
  2192.  
  2193. /* Compile stack routine for regex_compile.  */
  2194.  
  2195. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and 
  2196.    false if it's not.  */
  2197.  
  2198. static boolean
  2199. is_in_compile_stack (compile_stack, regnum)
  2200.   compile_stack_type compile_stack;
  2201.   int regnum;
  2202. {
  2203.   int this_element;
  2204.   
  2205.   if (COMPILE_STACK_EMPTY)
  2206.     return false;
  2207.  
  2208.   for (this_element = compile_stack.avail - 1;  
  2209.        this_element >= 0; 
  2210.        this_element--)
  2211.     if (compile_stack.stack[this_element].regnum == regnum)
  2212.       return true;
  2213.  
  2214.   return false;
  2215. }
  2216.  
  2217.  
  2218. /* Pattern offsets list stuff.  */
  2219.  
  2220. /* Initializes a pattern offsets list PATTERN_OFFSETS_LIST_PTR to be 
  2221.    INIT_SIZE large.  
  2222.    
  2223.    Returns 1 if it can allocate the space and 0 if it can't.   */
  2224.  
  2225. static unsigned
  2226. init_pattern_offsets_list (pattern_offsets_list_ptr, init_size)
  2227.   pattern_offsets_list_type *pattern_offsets_list_ptr;
  2228.   int init_size;
  2229. {
  2230.   if (init_size < 0)
  2231.     {
  2232.       printf ("Can't initialize a pattern offsets list with a negative \
  2233. or zero init_size %d.\n", init_size);
  2234.       exit (1);
  2235.     }
  2236.   else
  2237.     {
  2238.       pattern_offsets_list_ptr->offsets 
  2239.         = (pattern_offset_type *) malloc (init_size
  2240.                                            * sizeof (pattern_offset_type));
  2241.  
  2242.       if (pattern_offsets_list_ptr->offsets == NULL)
  2243.         return 0;
  2244.         
  2245.       pattern_offsets_list_ptr->size = init_size;
  2246.       pattern_offsets_list_ptr->avail = 0;
  2247.     }
  2248.   return 1;
  2249. }
  2250.  
  2251.  
  2252. /* Doubles the size of a pattern offsets list PATTERN_OFFSETS_LIST_PTR. 
  2253.    
  2254.    Returns 1 if it can allocate the space and 0 if it can't.   */
  2255.  
  2256. static unsigned
  2257. double_pattern_offsets_list (pattern_offsets_list_ptr)
  2258.   pattern_offsets_list_type *pattern_offsets_list_ptr;
  2259. {
  2260.   pattern_offsets_list_ptr->offsets 
  2261.     = (pattern_offset_type *) realloc (pattern_offsets_list_ptr->offsets, 
  2262.       (pattern_offsets_list_ptr->size << 1) * sizeof (pattern_offset_type));
  2263.  
  2264.   if (pattern_offsets_list_ptr->offsets == NULL)
  2265.     return 0;
  2266.  
  2267.   pattern_offsets_list_ptr->size <<= 1;
  2268.   return 1;
  2269. }
  2270.  
  2271.  
  2272. /* Adds OFFSET to PATTERN_OFFSETS_LIST_PTR.
  2273.  
  2274.    Returns 1 if it can add the offset and 0 if it needs to allocate
  2275.    space for it and can't.  */
  2276.  
  2277. static unsigned
  2278. add_pattern_offset (pattern_offsets_list_ptr, offset)
  2279.   pattern_offsets_list_type *pattern_offsets_list_ptr;
  2280.   pattern_offset_type offset;
  2281. {
  2282.   if (PATTERN_OFFSETS_LIST_PTR_FULL (pattern_offsets_list_ptr))  
  2283.     if (!double_pattern_offsets_list (pattern_offsets_list_ptr))
  2284.       return 0;
  2285.       
  2286.   pattern_offsets_list_ptr->offsets[pattern_offsets_list_ptr->avail] = offset;
  2287.   pattern_offsets_list_ptr->avail++;
  2288.   
  2289.   return 1;
  2290. }
  2291.  
  2292.    
  2293. /*  Adjust each offset in PATTERN_OFFSETS_LIST_PTR by INCREMENT.  */
  2294.  
  2295. static void
  2296. adjust_pattern_offsets_list (increment, start_position, 
  2297.                  pattern_offsets_list_ptr)
  2298.   unsigned increment;
  2299.   unsigned start_position;
  2300.   pattern_offsets_list_type *pattern_offsets_list_ptr;
  2301. {
  2302.   unsigned this_pattern_offset = 0;
  2303.   
  2304.   while (this_pattern_offset < pattern_offsets_list_ptr->avail  
  2305.      && pattern_offsets_list_ptr->offsets[this_pattern_offset] 
  2306.             < start_position)
  2307.     this_pattern_offset++;
  2308.     
  2309.   for (; this_pattern_offset < pattern_offsets_list_ptr->avail; 
  2310.        this_pattern_offset++)
  2311.     pattern_offsets_list_ptr->offsets[this_pattern_offset] += increment;
  2312. }
  2313.  
  2314.  
  2315. /* Anchor routines for regex_compile.  */
  2316.  
  2317. /* If it's in a group, record in ANCHOR_LIST_PTR an anchor offset that's 
  2318.    at OFFSET.  
  2319.    
  2320.    Returns 1 if can put the offset in ANCHOR_LIST_PTR.
  2321.    Returns 0 if runs out of memory allocating space for it.  */
  2322.    
  2323. static unsigned
  2324. record_anchor_position (in_a_group, offset, anchor_list_ptr)
  2325.   unsigned in_a_group;
  2326.   pattern_offset_type offset;
  2327.   anchor_list_type *anchor_list_ptr;
  2328. {
  2329.   if (in_a_group)
  2330.     if (!add_pattern_offset (anchor_list_ptr, offset))
  2331.       return 0;
  2332.  
  2333.   return 1;
  2334. }
  2335.  
  2336.  
  2337. /* Set all `begline's between START and END in BUFP to `no_op's.
  2338.    Set all such `endline's to either `endline_in_repeat's and all such
  2339.    `endline_before_newline's to `repeated_endline_before_repeat's.  */
  2340.  
  2341. static void
  2342. remove_intervening_anchors (start, end, anchor_list, bufp)
  2343.   char *start, *end;
  2344.   anchor_list_type anchor_list;
  2345.   struct re_pattern_buffer *bufp;
  2346. {
  2347.   unsigned this_anchor = 0;
  2348.  
  2349.   while (this_anchor < anchor_list.avail
  2350.          && start - bufp->buffer <= anchor_list.offsets[this_anchor]
  2351.          && anchor_list.offsets[this_anchor] <= end - bufp->buffer)
  2352.     {
  2353.       char *this_anchor_ptr 
  2354.         = bufp->buffer + anchor_list.offsets[this_anchor++];
  2355.       
  2356.       *this_anchor_ptr = *this_anchor_ptr == endline 
  2357.              ? (char)endline_in_repeat 
  2358.                          : *this_anchor_ptr == endline_before_newline
  2359.                            ? (char)repeated_endline_before_newline
  2360.                        : *this_anchor_ptr == begline
  2361.                  ? (char)no_op
  2362.                              : *this_anchor_ptr;
  2363.     }
  2364. }
  2365.  
  2366.  
  2367. /* Op list stuff.  */
  2368.  
  2369. /* Add OP_OFFSET to OP_LIST_PTR.  
  2370.    Return 1 if can add it and 0 if can't allocate the space to do so.  */
  2371.  
  2372. static unsigned
  2373. add_op (op_list_ptr, op_offset)
  2374.   op_list_type *op_list_ptr;
  2375.   pattern_offset_type op_offset;
  2376. {
  2377.   return add_pattern_offset (op_list_ptr, op_offset);
  2378. }
  2379.  
  2380.  
  2381. /* Verify that all `$'s in an entire pattern buffer BUFP are valid
  2382.    anchors or ordinary characters.  Either leave or change intermediate
  2383.    forms of `$' anchor ops into `endline' or `exactn ...' where
  2384.    appropriate.
  2385.  
  2386.    Return true in ENOUGH_MEMORY if don't run out of space allocating
  2387.    internal data structures.
  2388.  
  2389.    Return from the routine true if the pattern is valid and false 
  2390.    if it isn't.  */
  2391.  
  2392. static boolean
  2393. verify_and_adjust_endlines (op_list, group_forward_match_status, 
  2394.                 bufp, enough_memory)
  2395.   op_list_type op_list;
  2396.     /* `duplicate' case needs this: which groups matched something;
  2397.         set when went fowards through the pattern.  */
  2398.   bits_list_type group_forward_match_status;    
  2399.   struct re_pattern_buffer *bufp;
  2400.   boolean *enough_memory;
  2401. {
  2402.   int this_op_offset;    /* Has to be type int because decrementing it.  */
  2403.   /* See comments for analogous variables used for '^' in regex_compile.  */
  2404.  
  2405.   bits_list_type level_match_status;
  2406.   unsigned current_level = 0;
  2407.   bits_list_type group_match_status;
  2408.   bits_list_type group_active_status;
  2409.   char *bend = bufp->buffer + bufp->used;
  2410.   char *previous_p = NULL;
  2411.  
  2412.  
  2413.   if (!(init_bits_list (&level_match_status) 
  2414.         && init_bits_list (&group_match_status)
  2415.         && init_bits_list (&group_active_status)))
  2416.     {
  2417.       *enough_memory = false;
  2418.       return true;
  2419.     }
  2420.   else
  2421.     *enough_memory = true;
  2422.  
  2423.   for (this_op_offset = op_list.avail - 1; this_op_offset >= 0; 
  2424.        this_op_offset--)  
  2425.     {
  2426.       char *p = bufp->buffer + op_list.offsets[this_op_offset];
  2427.  
  2428.       if (!enough_memory)
  2429.         break;
  2430.  
  2431.       switch ((enum regexpcode) *p)
  2432.         {
  2433.           case endline:
  2434.           case endline_in_repeat:
  2435.           case endline_before_newline:
  2436.           case repeated_endline_before_newline:
  2437.  
  2438.            /* If the '$' must be at the pattern's end or else is
  2439.               in a trailing position.  */
  2440.        
  2441.             if ((bufp->syntax & RE_ANCHORS_ONLY_AT_ENDS) 
  2442.                  ? p + 1 == bend
  2443.          : ((bufp->syntax & RE_TIGHT_ALT)
  2444.                     ? p + 3 == bend  /* Would have two following no_ops.  */
  2445.                     : (*p == endline_before_newline
  2446.                        || *p == repeated_endline_before_newline
  2447.                        || no_levels_match_anything (level_match_status))))
  2448.           {
  2449.                 if ((enum regexpcode) *p == endline_in_repeat
  2450.                     || (enum regexpcode) *p == repeated_endline_before_newline)
  2451.                   if (bufp->syntax & RE_REPEATED_ANCHORS_AWAY)
  2452.                     *p = no_op;
  2453.               else
  2454.             *p = endline;
  2455.         
  2456.  
  2457.                 /* If this is a trailing '$' in an empty alternative.  */
  2458.  
  2459.                 if ((bufp->syntax & RE_NO_EMPTY_ALTS)
  2460.     
  2461.                     /* If there's an alternation op right before this `$'.  */
  2462.                     && ((this_op_offset > 0 
  2463.              && *(bufp->buffer 
  2464.                           + op_list.offsets[this_op_offset - 1])
  2465.                             == jump_past_next_alt)
  2466.     
  2467.                      /* Or this `$' is the only thing in the first
  2468.                         alternative of more than one of them.  */
  2469.     
  2470.                         || ((this_op_offset == 0   /* It's first.  */
  2471.                  /* Or it's right after an open-group op.  */
  2472.                              || (this_op_offset > 0 
  2473.                  && *(bufp->buffer 
  2474.                                   + op_list.offsets[this_op_offset - 1])
  2475.                                     == start_memory)) 
  2476.  
  2477.                             /* And it's right before an alternation op.  */
  2478.                         && previous_p != NULL 
  2479.                             && *previous_p == jump_past_next_alt)))
  2480.                   return false;
  2481.               }
  2482.     
  2483.             else if (bufp->syntax & RE_CONTEXT_INVALID_ANCHORS)
  2484.           return false;
  2485.  
  2486.             else if (!(bufp->syntax & RE_CONTEXT_INDEP_ANCHORS))
  2487.               {
  2488.                 p[0] = (char)exactn; 
  2489.                 p[1] = (char)1;
  2490.                 p[2] = '$';
  2491.               }
  2492.               
  2493.             break;
  2494.  
  2495.  
  2496.           /* Yes, start and stop_memory are switched because we're going
  2497.              backwards through the pattern!  */
  2498.     
  2499.           case stop_memory:
  2500.             increase_level (¤t_level);
  2501.  
  2502.             if (!make_group_active (&group_active_status, p[1]))
  2503.           enough_memory = false;          
  2504.  
  2505.             break;
  2506.  
  2507.           case start_memory:
  2508.             if (get_level_match_status (level_match_status, current_level))
  2509.               if (!set_next_lower_level (&level_match_status, current_level))
  2510.                   enough_memory = false;
  2511.               else
  2512.                 {
  2513.                   decrease_level (¤t_level);
  2514.                   make_group_inactive (&group_active_status, p[1]);
  2515.                 }
  2516.       
  2517.             break;               
  2518.  
  2519.  
  2520.           /* Hit an alternative.  */
  2521.  
  2522.           case jump_past_next_alt:
  2523.             if (lower_levels_match_nothing (level_match_status, current_level))
  2524.               clear_this_and_higher_levels (&level_match_status,current_level);
  2525.  
  2526.             break;
  2527.  
  2528.       /* These below mean was followed by a repetition operator.  */
  2529.       case no_op:
  2530.           case maybe_pop_jump:
  2531.       case no_pop_jump_n:
  2532.         if (bufp->syntax & RE_REPEATED_ANCHORS_AWAY)
  2533.               break;
  2534.           case charset:
  2535.       case charset_not:
  2536.       case wordchar:
  2537.       case notwordchar:
  2538.       case exactn:
  2539.           case anychar:
  2540.             if (!set_this_level (&level_match_status, current_level)
  2541.                 || !set_match_status_of_active_groups (group_active_status,
  2542.                                                        &group_match_status))
  2543.               enough_memory = false;;
  2544.  
  2545.             break;
  2546.  
  2547.       case duplicate: 
  2548.             /* Only set level_match_status if this back reference
  2549.                refers to a nonempty group.  */
  2550.  
  2551.             if (get_group_match_status (group_forward_match_status, p[1]))
  2552.               if (!set_this_level (&level_match_status, current_level))
  2553.                 enough_memory = false;
  2554.  
  2555.             break;
  2556.                 
  2557.           default: 
  2558.             printf ("Found an unknown operator %u in compiled pattern.\n", *p);
  2559.         }
  2560.       previous_p = p;
  2561.     }
  2562.   return true;
  2563. }
  2564.  
  2565.  
  2566.  
  2567. /* Bits list routines.  (See above for macros.)  */
  2568.  
  2569. /* Initialize BITS_LIST_PTR to have one bits block.  
  2570.    Return 1 if there's enough memory to do so and 0 if there isn't.  */
  2571.  
  2572. static unsigned
  2573. init_bits_list (bits_list_ptr)
  2574.   bits_list_type *bits_list_ptr;
  2575. {
  2576.   bits_list_ptr->bits = (unsigned *) malloc (sizeof (unsigned));
  2577.  
  2578.   if (bits_list_ptr->bits == NULL)
  2579.     return 0;
  2580.  
  2581.   bits_list_ptr->size = BITS_BLOCK_SIZE;
  2582.   bits_list_ptr->bits[0] = 0;
  2583.  
  2584.   return 1;
  2585. }
  2586.  
  2587.  
  2588. /* Extend BITS_LIST_PTR by one bits block.
  2589.    Return 1 if there's enough memory to do so and 0 if there isn't.  */
  2590.  
  2591. static unsigned
  2592. extend_bits_list (bits_list_ptr)
  2593.   bits_list_type *bits_list_ptr;
  2594. {
  2595.   bits_list_ptr->bits
  2596.     = (unsigned *) realloc (bits_list_ptr->bits, 
  2597.                 bits_list_ptr->size + sizeof (unsigned));
  2598.  
  2599.   if (bits_list_ptr->bits == NULL)
  2600.     return 0;
  2601.  
  2602.   bits_list_ptr->size += BITS_BLOCK_SIZE;
  2603.   bits_list_ptr->bits[(bits_list_ptr->size/BITS_BLOCK_SIZE) - 1] = 0;
  2604.  
  2605.   return 1;
  2606. }
  2607.  
  2608.  
  2609. /* Get the bit value at a positive POSITION in BITS_LIST.  */
  2610.  
  2611. static unsigned
  2612. get_bit (bits_list, position)
  2613.   bits_list_type bits_list;
  2614.   unsigned position;
  2615. {
  2616.   if (position < 0)
  2617.     {
  2618.       printf ("Tried to get a bit at position less than zero.\n");
  2619.       exit (1);
  2620.     }
  2621.  
  2622.   if (position > bits_list.size - 1)
  2623.     {
  2624.       printf ("Getting bit value: position %d exceeds bits list size %d.\n", 
  2625.           position, bits_list.size);
  2626.       exit (1);
  2627.     }
  2628.  
  2629.   return bits_list.bits[BITS_BLOCK (position)] & BITS_MASK (position);
  2630. }
  2631.  
  2632.  
  2633. /* Set the bit for a positive POSITION in BITS_LIST_PTR to VALUE, which,
  2634.    in turn, can only be 0 or 1.  
  2635.    
  2636.    Returns 1 if can set the bit and 0 if ran out of memory allocating
  2637.    (if necessary) room for it.  */
  2638.    
  2639. static unsigned
  2640. set_bit_to_value (bits_list_ptr, position, value)
  2641.   bits_list_type *bits_list_ptr;
  2642.   unsigned position;
  2643.   unsigned value;
  2644. {
  2645.   if (position < 0)
  2646.     {
  2647.       printf ("Tried to set a bit at position less than zero.\n");
  2648.       exit (1);
  2649.     }
  2650.  
  2651.   if (position > bits_list_ptr->size - 1
  2652.       && !extend_bits_list (bits_list_ptr))
  2653.     return 0;
  2654.  
  2655.   if (value == 1)
  2656.     bits_list_ptr->bits[BITS_BLOCK (position)] |= BITS_MASK (position);
  2657.   else if (value == 0)
  2658.     bits_list_ptr->bits[BITS_BLOCK (position)] &= ~(BITS_MASK (position));
  2659.   else
  2660.     {
  2661.       printf ("Invalid value %d to set a bit.\n");
  2662.       exit (1);
  2663.     }
  2664.   return 1;
  2665. }
  2666.  
  2667.  
  2668. /* Level stuff.  */
  2669.  
  2670.  
  2671. /* Return 1 if LEVEL in LEVEL_MATCH_STATUS matches something and
  2672.    0 if it doesn't.  Assumes LEVEL is positive.   */
  2673.  
  2674. static unsigned
  2675. get_level_match_status (level_match_status, level)
  2676.   bits_list_type level_match_status;
  2677.   unsigned level;
  2678. {
  2679.   return get_bit (level_match_status, level);
  2680. }
  2681.  
  2682.  
  2683. /* Mark as matching something the level LEVEL in LEVEL_MATCH_STATUS_PTR.
  2684.    Assumes LEVEL is positive. 
  2685.    
  2686.    Return 1 if can mark the level and 0 if need to allocate space for it
  2687.    but can't.  */
  2688.  
  2689. static unsigned
  2690. set_this_level (level_match_status_ptr, level)
  2691.   bits_list_type *level_match_status_ptr;
  2692.   unsigned level;
  2693. {
  2694.   return set_bit_to_value (level_match_status_ptr, level, 1);
  2695. }
  2696.  
  2697.  
  2698. /* Mark as matching something the level below the LEVEL recorded in
  2699.    LEVEL_MATCH_STATUS_PTR.  Assumes LEVEL is greater than zero.
  2700.    
  2701.    Return 1 if can mark the level and 0 ran out of memory trying to do so.  */
  2702.  
  2703. static unsigned
  2704. set_next_lower_level (level_match_status_ptr, level)
  2705.   bits_list_type *level_match_status_ptr;
  2706.   unsigned level;
  2707. {
  2708.   unsigned this_level;
  2709.  
  2710.   return set_bit_to_value (level_match_status_ptr, level - 1, 1);
  2711. }
  2712.  
  2713.  
  2714. /* Mark as matching something the level LEVEL and all levels higher than
  2715.    it currently in LEVEL_MATCH_STATUS_PTR.  Assumes LEVEL is positive.
  2716.  
  2717.    Return 1 if can mark the levels and 0 ran out of memory trying to do so.  */
  2718.  
  2719. static void
  2720. clear_this_and_higher_levels (level_match_status_ptr, level)
  2721.   bits_list_type *level_match_status_ptr;
  2722.   unsigned level;
  2723. {
  2724.   unsigned this_level;
  2725.  
  2726.   for (this_level = level; 
  2727.        this_level < level_match_status_ptr->size; 
  2728.        this_level++)
  2729.     set_bit_to_value (level_match_status_ptr, this_level, 0);
  2730. }
  2731.  
  2732.  
  2733. /* Returns true if none of the levels in LEVEL_MATCH_STATUS less than a
  2734.    positive LEVEL match anything, and false otherwise.  */
  2735.  
  2736. static boolean
  2737. lower_levels_match_nothing (level_match_status, level)
  2738.   bits_list_type level_match_status;
  2739.   unsigned level;
  2740. {
  2741.   unsigned this_level;
  2742.   
  2743.   for (this_level = 0; this_level < level; this_level++)
  2744.     if (get_bit (level_match_status, this_level))
  2745.       return false;
  2746.  
  2747.   return true;
  2748. }
  2749.  
  2750. /* Returns true if none of the levels in LEVEL_MATCH_STATUS match
  2751.    anything, and false otherwise.  */
  2752.  
  2753. static boolean
  2754. no_levels_match_anything (level_match_status)
  2755.   bits_list_type level_match_status;
  2756. {
  2757.   unsigned this_bits_block;
  2758.   
  2759.   for (this_bits_block = 0; 
  2760.        this_bits_block < level_match_status.size/BITS_BLOCK_SIZE;
  2761.        this_bits_block++)
  2762.     if (level_match_status.bits[this_bits_block] != 0)
  2763.       return false;
  2764.  
  2765.   return true;
  2766. }
  2767.  
  2768.  
  2769. /* Increase CURRENT_LEVEL_PTR.  */
  2770.  
  2771. static void
  2772. increase_level (current_level_ptr)
  2773.   unsigned *current_level_ptr;
  2774. {
  2775.   (*current_level_ptr)++;
  2776. }
  2777.  
  2778.  
  2779. /* Decrease CURRENT_LEVEL_PTR, but exit on error if try to decrease
  2780.    below zero.  */
  2781.  
  2782. static void
  2783. decrease_level (current_level_ptr)
  2784.   unsigned *current_level_ptr;
  2785. {
  2786.   if (*current_level_ptr == 0)
  2787.     {
  2788.       printf ("Tried to decrease current level below zero.\n");
  2789.       exit (1);
  2790.     }
  2791.   (*current_level_ptr)--;
  2792. }
  2793.  
  2794.  
  2795. /* Group stuff.  */
  2796.  
  2797.  
  2798. /* Mark a positive GROUP in GROUP_ACTIVE_STATUS_PTR as active.  
  2799.    Return 1 if can mark the group and 0 ran out of memory trying to do so.  */
  2800.  
  2801. static unsigned
  2802. make_group_active (group_active_status_ptr, group)
  2803.   bits_list_type *group_active_status_ptr;
  2804.   unsigned group;
  2805. {
  2806.   return set_bit_to_value (group_active_status_ptr, group, 1);
  2807. }
  2808.  
  2809.  
  2810. /* Mark a positive GROUP in GROUP_ACTIVE_STATUS_PTR as inactive.  
  2811.    Return 1 if can mark the group and 0 ran out of memory trying to do so.  */
  2812.  
  2813. static unsigned
  2814. make_group_inactive (group_active_status_ptr, group)
  2815.   bits_list_type *group_active_status_ptr;
  2816.   unsigned group;
  2817. {
  2818.   return set_bit_to_value (group_active_status_ptr, group, 0);
  2819. }
  2820.  
  2821.  
  2822. /* Mark as active in GROUP_MATCH_STATUS_PTR those active groups recorded
  2823.    in GROUP_ACTIVE_STATUS_PTR.
  2824.  
  2825.    Return 1 if can mark the groups and 0 ran out of memory trying to do so.  */
  2826.  
  2827. static unsigned
  2828. set_match_status_of_active_groups (group_active_status, group_match_status_ptr)
  2829.   bits_list_type group_active_status;
  2830.   bits_list_type *group_match_status_ptr;
  2831. {
  2832.   unsigned this_bit_block;
  2833.  
  2834.   if (group_active_status.size > group_match_status_ptr->size
  2835.       && !extend_bits_list (group_match_status_ptr))
  2836.     return 0;
  2837.   
  2838.   for (this_bit_block = 0; 
  2839.        this_bit_block < group_active_status.size/BITS_BLOCK_SIZE;
  2840.        this_bit_block++)
  2841.     group_match_status_ptr->bits[this_bit_block] 
  2842.       |= group_active_status.bits[this_bit_block];
  2843.  
  2844.   return 1;
  2845. }
  2846.  
  2847.  
  2848. /* Return 1 if GROUP in GROUP_MATCH_STATUS matches something and
  2849.    0 if it doesn't.  Assumes GROUP is positive.   */
  2850.  
  2851. static unsigned
  2852. get_group_match_status (group_match_status, group)
  2853.   bits_list_type group_match_status;
  2854.   unsigned group;
  2855. {
  2856.   return get_bit (group_match_status, group);
  2857. }
  2858.  
  2859.  
  2860.  
  2861.  
  2862.  
  2863. /* Failure stack declarations and macros for both re_compile_fastmap and
  2864.    re_match_2.  Have to use `alloca' for reasons stated in INIT_BITS_LIST's
  2865.    comment.  */
  2866.    
  2867.  
  2868. /* Roughly the maximum number of failure points on the stack.  Would be
  2869.    exactly that if always used MAX_FAILURE_SPACE each time we failed.  */
  2870.    
  2871. int re_max_failures = 2000;
  2872.  
  2873.  
  2874. typedef unsigned char *failure_stack_element;
  2875.  
  2876. typedef struct {
  2877.     failure_stack_element *stack;
  2878.     unsigned size;
  2879.     unsigned avail;            /* Offset of next open position.  */
  2880.   } failure_stack_type;
  2881.  
  2882.  
  2883. #define FAILURE_STACK_EMPTY (failure_stack.avail == 0)
  2884. #define FAILURE_STACK_PTR_EMPTY  (failure_stack_ptr->avail == 0)
  2885. #define FAILURE_STACK_FULL (failure_stack.avail == failure_stack.size)
  2886.  
  2887.  
  2888. /* Initialize a failure stack.
  2889.  
  2890.    Return 1 if was able to allocate the space for (FAILURE_STACK) and
  2891.    0 if not.  */
  2892.  
  2893. #define INIT_FAILURE_STACK(failure_stack)                \
  2894.   ((failure_stack).stack = (failure_stack_element *)            \
  2895.     REGEX_ALLOCATE (INIT_FAILURE_ALLOC * sizeof (failure_stack_element)),\
  2896.                                     \
  2897.   (failure_stack).stack == NULL                        \
  2898.   ? 0                                    \
  2899.   : ((failure_stack).size = INIT_FAILURE_ALLOC,                \
  2900.      (failure_stack).avail = 0,                        \
  2901.      1))
  2902.  
  2903.  
  2904. /* Double the size of FAILURE_STACK, up to MAX_SIZE.  
  2905.  
  2906.    Return 1 if was able to double it, and 0 if either ran out of memory
  2907.    allocating space for it or it was already MAX_SIZE large.  
  2908.    
  2909.    REGEX_REALLOCATE requires `void *destination' be declared.   */
  2910.  
  2911. #define DOUBLE_FAILURE_STACK(failure_stack, max_size)            \
  2912.   ((failure_stack).size > max_size                    \
  2913.    ? 0                                    \
  2914.    : ((failure_stack).stack = (failure_stack_element *)            \
  2915.         REGEX_REALLOCATE ((failure_stack).stack,             \
  2916.           ((failure_stack).size << 1) * sizeof (failure_stack_element)),\
  2917.                                     \
  2918.       (failure_stack).stack == NULL                    \
  2919.       ? 0                                \
  2920.       : ((failure_stack).size <<= 1,                     \
  2921.          1)))
  2922.  
  2923.  
  2924. /* Push PATTERN_OP on (FAILURE_STACK). 
  2925.  
  2926.    Return 1 if was able to do so and 0 if ran out of memory allocating
  2927.    space to do so.
  2928.  
  2929.    DOUBLE_FAILURE_STACK requires `void *destination' be declared.   */
  2930.  
  2931. #define PUSH_PATTERN_OP(pattern_op, failure_stack)            \
  2932.   ((FAILURE_STACK_FULL                            \
  2933.     && !DOUBLE_FAILURE_STACK (failure_stack, re_max_failures))        \
  2934.     ? 0                                    \
  2935.     : ((failure_stack).stack[(failure_stack).avail++] = pattern_op,    \
  2936.        1))
  2937.  
  2938.  
  2939. /* Push most of the information about the state we will want
  2940.    if we ever fail back to it.  
  2941.    
  2942.    Requires regstart, regend, reg_info, and num_internal_regs be declared.
  2943.    DOUBLE_FAILURE_STACK requires `void *destination' be declared.   
  2944.    
  2945.    Does a `return FAILURE_CODE' if runs out of memory.  */
  2946.  
  2947. #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_stack, failure_code)                \
  2948.   do {                                    \
  2949.     long highest_used_reg, this_reg;                    \
  2950.     void *destination;                            \
  2951.                                                                         \
  2952.     /* Find out how many registers are active or have been matched.    \
  2953.        (Aside from register zero, which is only set at the end.)  */    \
  2954.                                     \
  2955.     for (highest_used_reg = num_internal_regs - 1; highest_used_reg > 0;\
  2956.          highest_used_reg--)                        \
  2957.       if (regstart[highest_used_reg] != (unsigned char *) -1)        \
  2958.         break;                                \
  2959.                                     \
  2960.     while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS)            \
  2961.       if (!DOUBLE_FAILURE_STACK (failure_stack,             \
  2962.           re_max_failures * MAX_FAILURE_ITEMS))                \
  2963.         return failure_code;                        \
  2964.                                     \
  2965.     /* Now push the info for each of those registers.  */        \
  2966.                                     \
  2967.     for (this_reg = 1; this_reg <= highest_used_reg; this_reg++)    \
  2968.       {                                    \
  2969.         (failure_stack).stack[(failure_stack).avail++]             \
  2970.           = regstart[this_reg];                        \
  2971.                                     \
  2972.         (failure_stack).stack[(failure_stack).avail++] = regend[this_reg];\
  2973.                                     \
  2974.         (failure_stack).stack[(failure_stack).avail++]             \
  2975.           = (unsigned char *) ®_info[this_reg];            \
  2976.       }                                    \
  2977.                                     \
  2978.     /* Push how many registers we saved.  */                \
  2979.     (failure_stack).stack[(failure_stack).avail++]            \
  2980.       = (unsigned char *) highest_used_reg;                \
  2981.                                     \
  2982.     (failure_stack).stack[(failure_stack).avail++] = pattern_place;    \
  2983.     (failure_stack).stack[(failure_stack).avail++] = string_place;    \
  2984.   } while (0)
  2985.  
  2986.  
  2987.  
  2988.  
  2989. /* Given a pattern, compute a fastmap from it.  The fastmap records
  2990.    which of the (1 << BYTEWIDTH) possible characters can start a string
  2991.    that matches the pattern.  This fastmap is used by re_search to skip
  2992.    quickly over totally impossible text.
  2993.  
  2994.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data 
  2995.    area as bufp->fastmap.
  2996.    The other components of bufp describe the pattern to be used.  
  2997.    
  2998.    Returns 0 if it can compile a fastmap.
  2999.    Returns -2 if there is an internal error.   */
  3000.  
  3001. int
  3002. re_compile_fastmap (bufp)
  3003.      struct re_pattern_buffer *bufp;
  3004. {
  3005.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  3006.   int size = bufp->used;
  3007.   register char *fastmap = bufp->fastmap;
  3008.   unsigned char *p = pattern;
  3009.   register unsigned char *pend = pattern + size;
  3010.   int j, k;
  3011.   unsigned char *translate = (unsigned char *) bufp->translate;
  3012.   failure_stack_type failure_stack;
  3013.   void *destination;
  3014.  
  3015.  
  3016.   INIT_FAILURE_STACK (failure_stack);
  3017.  
  3018.   bzero (fastmap, (1 << BYTEWIDTH));
  3019.   bufp->fastmap_accurate = 1;
  3020.   bufp->can_be_null = 0;
  3021.       
  3022.   while (p)
  3023.     {
  3024.       boolean is_a_succeed_n = false;
  3025.       
  3026.       if (p == pend)
  3027.         if (FAILURE_STACK_EMPTY)      
  3028.           {
  3029.             bufp->can_be_null = 1;
  3030.             break;
  3031.           }
  3032.         else
  3033.           p = failure_stack.stack[--failure_stack.avail];
  3034.           
  3035.         
  3036. #ifdef SWITCH_ENUM_BUG
  3037.       switch ((int) ((enum regexpcode) *p++))
  3038. #else
  3039.       switch ((enum regexpcode) *p++)
  3040. #endif
  3041.     {
  3042.     case exactn:
  3043.           fastmap[translate ? translate[p[1]] : p[1]] = 1;
  3044.       break;
  3045.  
  3046.         case begline:
  3047.         case before_dot:
  3048.     case at_dot:
  3049.     case after_dot:
  3050.     case begbuf:
  3051.     case endbuf:
  3052.     case wordbound:
  3053.     case notwordbound:
  3054.     case wordbeg:
  3055.     case wordend:
  3056.           continue;
  3057.  
  3058.     case endline:
  3059.           fastmap[translate ? translate['\n'] : '\n'] = 1;
  3060.             
  3061.       if (! bufp->can_be_null)
  3062.         bufp->can_be_null = 2;
  3063.       break;
  3064.  
  3065.     case no_pop_jump_n:
  3066.         case pop_failure_jump:
  3067.     case maybe_pop_jump:
  3068.     case no_pop_jump:
  3069.         case jump_past_next_alt:
  3070.     case dummy_failure_jump:
  3071.           extract_number_and_incr (&j, &p);
  3072.       p += j;    
  3073.       if (j > 0)
  3074.         continue;
  3075.             
  3076.           /* Jump backward reached implies we just went through
  3077.              the body of a loop and matched nothing.  Opcode jumped to
  3078.              should be an on_failure_jump or succeed_n.  Just treat it
  3079.              like an ordinary jump.  For a * loop, it has pushed its
  3080.              failure point already; If so, discard that as redundant.  */
  3081.  
  3082.           if ((enum regexpcode) *p != on_failure_jump
  3083.           && (enum regexpcode) *p != succeed_n)
  3084.         continue;
  3085.  
  3086.           p++;
  3087.           extract_number_and_incr (&j, &p);
  3088.           p += j;        
  3089.       
  3090.           /* If what's on the stack is where we are now, pop it.  */
  3091.           
  3092.           if (!FAILURE_STACK_EMPTY 
  3093.           && failure_stack.stack[failure_stack.avail - 1] == p)
  3094.             failure_stack.avail--;
  3095.             
  3096.           continue;
  3097.       
  3098.         case on_failure_jump:
  3099.     handle_on_failure_jump:
  3100.           extract_number_and_incr (&j, &p);
  3101.  
  3102.           if (!PUSH_PATTERN_OP (p + j, failure_stack))
  3103.             return -2;
  3104.  
  3105.           if (is_a_succeed_n)
  3106.             extract_number_and_incr (&k, &p);    /* Skip the n.  */
  3107.  
  3108.           continue;
  3109.  
  3110.     case succeed_n:
  3111.       is_a_succeed_n = true;
  3112.           /* Get to the number of times to succeed.  */
  3113.           p += 2;        
  3114.       /* Increment p past the n for when k != 0.  */
  3115.           extract_number_and_incr (&k, &p);
  3116.           if (k == 0)
  3117.         {
  3118.               p -= 4;
  3119.               goto handle_on_failure_jump;
  3120.             }
  3121.           continue;
  3122.           
  3123.     case set_number_at:
  3124.           p += 4;
  3125.           continue;
  3126.  
  3127.         case start_memory:
  3128.     case stop_memory:
  3129.       p++;
  3130.       continue;
  3131.  
  3132.     case duplicate:
  3133.       bufp->can_be_null = 1;
  3134.       fastmap['\n'] = 1;
  3135.     case anychar:
  3136.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  3137.         if (j != '\n')
  3138.           fastmap[j] = 1;
  3139.       if (bufp->can_be_null)
  3140.         return 0;
  3141.       /* Don't return; check the alternative paths
  3142.          so we can set can_be_null if appropriate.  */
  3143.       break;
  3144.  
  3145.     case wordchar:
  3146.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  3147.         if (SYNTAX (j) == Sword)
  3148.           fastmap[j] = 1;
  3149.       break;
  3150.  
  3151.     case notwordchar:
  3152.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  3153.         if (SYNTAX (j) != Sword)
  3154.           fastmap[j] = 1;
  3155.       break;
  3156.  
  3157. #ifdef emacs
  3158.     case syntaxspec:
  3159.       k = *p++;
  3160.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  3161.         if (SYNTAX (j) == (enum syntaxcode) k)
  3162.           fastmap[j] = 1;
  3163.       break;
  3164.  
  3165.     case notsyntaxspec:
  3166.       k = *p++;
  3167.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  3168.         if (SYNTAX (j) != (enum syntaxcode) k)
  3169.           fastmap[j] = 1;
  3170.       break;
  3171. #endif /* not emacs */
  3172.  
  3173.         case charset:
  3174.           for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  3175.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  3176.               fastmap[translate ? translate[j] : j] = 1;
  3177.       break;
  3178.  
  3179.     case charset_not:
  3180.       /* Chars beyond end of map must be allowed.  */
  3181.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  3182.             fastmap[translate ? translate[j] : j] = 1;
  3183.  
  3184.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  3185.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  3186.               fastmap[translate ? translate[j] : j] = 1;
  3187.  
  3188.           break;
  3189.     } /* End switch *p++.  */
  3190.  
  3191.       /* Getting here means we have successfully found the possible starting
  3192.          characters of one path of the pattern.  We need not follow this
  3193.          path any farther.  Instead, look at the next alternative
  3194.          remembered in the stack.  */
  3195.  
  3196.       if (!FAILURE_STACK_EMPTY)
  3197.         p = failure_stack.stack[--failure_stack.avail];
  3198.       else
  3199.     break;
  3200.     }
  3201.   return 0;
  3202. } /* re_compile_fastmap  */
  3203.  
  3204.  
  3205.  
  3206.  
  3207. /* Like re_search_2, below, but only one string is specified, and
  3208.    doesn't let you say where to stop matching. */
  3209.  
  3210. int
  3211. re_search (bufp, string, size, startpos, range, regs)
  3212.      struct re_pattern_buffer *bufp;
  3213.      const char *string;
  3214.      const int size, startpos, range;
  3215.      struct re_registers *regs;
  3216. {
  3217.   return re_search_2 (bufp, (char *) 0, 0, string, size, startpos, range, 
  3218.               regs, size);
  3219. }
  3220.  
  3221.  
  3222. /* Using the compiled pattern in BUFP->buffer, first tries to match the
  3223.    virtual concatenation of STRING1 and STRING2, starting first at index
  3224.    STARTPOS, then at STARTPOS + 1, and so on.  RANGE is the number of
  3225.    places to try before giving up.  If RANGE is negative, it searches
  3226.    backwards, i.e., the starting positions tried are STARTPOS, STARTPOS - 1, 
  3227.    etc.  STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
  3228.    In REGS, return the indices of the virtual concatenation of STRING1
  3229.    and STRING2 that matched the entire BUFP->buffer and its contained
  3230.    subexpressions.  Do not consider matching one past the index MSTOP in
  3231.    the virtual concatenation of STRING1 and STRING2.
  3232.  
  3233.    The value returned is the position in the strings at which the match
  3234.    was found, -1 if no match was found, or -2 if error (such as
  3235.    failure stack overflow).  */
  3236.  
  3237. int
  3238. re_search_2 (bufp, string1, size1, string2, size2, startpos, range,
  3239.          regs, stop)
  3240.      struct re_pattern_buffer *bufp;
  3241.      const char *string1, *string2;
  3242.      const int size1, size2;
  3243.      const int startpos;
  3244.      const int range;
  3245.      struct re_registers *regs;
  3246.      const int stop;
  3247. {
  3248.   register char *fastmap = bufp->fastmap;
  3249.   register unsigned char *translate = (unsigned char *) bufp->translate;
  3250.   int total_size = size1 + size2;
  3251.   int private_startpos = startpos;
  3252.   int private_endpos = startpos + range;
  3253.   int private_range = range;
  3254.   int val;
  3255.   const struct re_pattern_buffer *private_bufp;
  3256.  
  3257.   /* Check for out-of-range starting position.  */
  3258.   if (private_startpos < 0  ||  private_startpos > total_size)
  3259.     return -1;
  3260.     
  3261.   /* Fix up range if it would eventually take private_startpos outside
  3262.      of the virtual concatenation of string1 and string2.  */
  3263.      
  3264.   if (private_endpos < -1)
  3265.     private_range = -1 - private_startpos;
  3266.  
  3267.   else if (private_endpos > total_size)
  3268.     private_range = total_size - private_startpos;
  3269.  
  3270.   
  3271. /* Update the fastmap now if not correct already.  */
  3272.   if (fastmap && !bufp->fastmap_accurate)
  3273.     if (re_compile_fastmap (bufp) == -2)
  3274.       return -2;
  3275.   
  3276.   /* If the search isn't to be a backwards one, don't waste time in a
  3277.      long search for a pattern that says it is anchored.  */
  3278.   if (bufp->used > 0 && (enum regexpcode) bufp->buffer[0] == begbuf
  3279.       && private_range > 0)
  3280.     {
  3281.       if (private_startpos > 0)
  3282.     return -1;
  3283.       else
  3284.     private_range = 1;
  3285.     }
  3286.  
  3287.   private_bufp = bufp;
  3288.  
  3289.   while (1)
  3290.     { 
  3291.       /* If a fastmap is supplied, skip quickly over characters that
  3292.          cannot possibly be the start of a match.  Note, however, that
  3293.          if the pattern can possibly match the null string, we don't
  3294.          want to skip over characters; we want the first null string we
  3295.          can match.  */
  3296.  
  3297.       if (fastmap && private_startpos < total_size && !bufp->can_be_null)
  3298.     {
  3299.       if (private_range > 0)    /* Searching forwards.  */
  3300.         {
  3301.           register int lim = 0;
  3302.           register unsigned char *p;
  3303.           int irange = private_range;
  3304.  
  3305.               if (private_startpos < size1 
  3306.                   && private_startpos + private_range >= size1)
  3307.                 lim = private_range - (size1 - private_startpos);
  3308.  
  3309.           p = ((unsigned char *)
  3310.            &(private_startpos >= size1 
  3311.                    ? string2 - size1 
  3312.                    : string1)[private_startpos]);
  3313.  
  3314.               while (private_range > lim && !fastmap[translate 
  3315.                                              ? translate[*p++]
  3316.                                              : *p++])
  3317.             private_range--;
  3318.           private_startpos += irange - private_range;
  3319.         }
  3320.       else                /* Searching backwards.  */
  3321.         {
  3322.           register unsigned char c;
  3323.  
  3324.               if (size1 == 0 || private_startpos >= size1)
  3325.         c = string2[private_startpos - size1];
  3326.           else 
  3327.         c = string1[private_startpos];
  3328.  
  3329.               c &= 0xff;
  3330.           if (translate ? !fastmap[translate[c]] : !fastmap[c])
  3331.         goto advance;
  3332.         }
  3333.     }
  3334.  
  3335.       if (private_range >= 0 && private_startpos == total_size
  3336.       && fastmap && bufp->can_be_null == 0)
  3337.     return -1;
  3338.  
  3339.       val = re_match_2 (private_bufp, string1, size1, string2, size2,
  3340.                     private_startpos, regs, stop);
  3341.       if (val >= 0)
  3342.     return private_startpos;
  3343.         
  3344.       if (val == -2)
  3345.     return -2;
  3346.  
  3347.     advance:
  3348.       if (!private_range) 
  3349.         break;
  3350.       else if (private_range > 0) 
  3351.         {
  3352.           private_range--; 
  3353.           private_startpos++;
  3354.         }
  3355.       else
  3356.         {
  3357.           private_range++; 
  3358.           private_startpos--;
  3359.         }
  3360.     }
  3361.   return -1;
  3362. }
  3363.  
  3364.  
  3365.  
  3366. #ifndef emacs   /* emacs never uses this.  */
  3367. int
  3368. re_match (bufp, string, size, pos, regs)
  3369.      const struct re_pattern_buffer *bufp;
  3370.      const char *string;
  3371.      const int size, pos;
  3372.      struct re_registers *regs;
  3373. {
  3374.   return re_match_2 (bufp, (char *) 0, 0, string, size, pos, regs, size); 
  3375. }
  3376. #endif /* not emacs */
  3377.  
  3378.  
  3379.  
  3380. /* Routines for re_match_2, defined below.  */
  3381.  
  3382. static boolean group_can_match_nothing ();
  3383. static int bcmp_translate ();
  3384.  
  3385.  
  3386. /* Macros used by re_match_2, defined below:  */
  3387.  
  3388. /* Structure and accessing macros used in re_match_2:  */
  3389.  
  3390. typedef struct register_info
  3391. {
  3392.   bits_list_type inner_groups;        /* Which groups are inside this one.  */
  3393.   int can_match_nothing;           /* Set if this group can match nothing;
  3394.                        -1 if not ever set.  */
  3395.   unsigned is_active : 1;
  3396.   unsigned matched_something : 1;
  3397.   unsigned ever_matched_something : 1;
  3398. } reg_info_type;
  3399.  
  3400.  
  3401. /* Macros used by re_match_2:  */
  3402. /* I.e., regstart, regend, and reg_info.  */
  3403.  
  3404. #define INNER_GROUPS(R)  ((R).inner_groups)
  3405. #define CAN_MATCH_NOTHING(R)  ((R).can_match_nothing)
  3406. #define IS_ACTIVE(R)  ((R).is_active)
  3407. #define MATCHED_SOMETHING(R)  ((R).matched_something)
  3408. #define EVER_MATCHED_SOMETHING(R)  ((R).ever_matched_something)
  3409.  
  3410.  
  3411. /* Record that group INNER is inside of all currently active groups.  */
  3412.  
  3413. #define NOTE_INNER_GROUP(inner)                        \
  3414.   do { unsigned this_reg;                        \
  3415.     for (this_reg = 0; this_reg < num_internal_regs; this_reg++)    \
  3416.       {                                    \
  3417.     void *destination;    /* For SET_BIT_TO_VALUE.  */        \
  3418.         int ret = SET_BIT_TO_VALUE (INNER_GROUPS (reg_info[this_reg]),    \
  3419.                     inner,                 \
  3420.                                     IS_ACTIVE(reg_info[this_reg]));     \
  3421.         if (ret == 0)                            \
  3422.           {                                \
  3423.             printf ("Ran out of memory in re_match_2 (NOTE_INNER_GROUP).\n");\
  3424.             exit (1);                            \
  3425.           }                                \
  3426.         if (ret != 1)                            \
  3427.           {                                \
  3428.             printf ("Invalid value %d to set a bit.\n", ret);        \
  3429.             exit (1);                            \
  3430.           }                                \
  3431.       }                                    \
  3432.   } while (0)
  3433.  
  3434.  
  3435. /* Call this when have matched something; it sets `matched' flags for the
  3436.    registers corresponding to the group of which we currently are inside.  
  3437.    Also records whether this group ever matched something.  */
  3438.  
  3439. #define SET_REGS_MATCHED                         \
  3440.   do { unsigned this_reg;                         \
  3441.     for (this_reg = 0; this_reg < num_internal_regs; this_reg++)    \
  3442.       {                                    \
  3443.         MATCHED_SOMETHING (reg_info[this_reg]) =            \
  3444.         EVER_MATCHED_SOMETHING (reg_info[this_reg]) =             \
  3445.           (IS_ACTIVE (reg_info[this_reg])) ? 1 : 0;            \
  3446.       }                                    \
  3447.   } while (0)
  3448.  
  3449.  
  3450.  
  3451. /* Failure stack macros for re_match_2.  */
  3452.  
  3453. /* This is the number of items that are pushed and popped on the stack
  3454.    for each register, i.e., its REGSTART, REGEND and REG_INFO.  */
  3455.  
  3456. #define NUM_REG_ITEMS  3
  3457.  
  3458. /* Refers to highest_used_reg (which we calculate), PATTERN_PLACE and
  3459.    STRING_PLACE, which are arguments to the PUSH_FAILURE_POINT macro.  */
  3460.    
  3461. #define NUM_OTHER_ITEMS 3
  3462.  
  3463. /* We put at most these many items on the stack whenever we push a
  3464.    failure point .  */
  3465.  
  3466. #define MAX_FAILURE_ITEMS                        \
  3467.   (num_internal_regs * NUM_REG_ITEMS + NUM_OTHER_ITEMS)
  3468.  
  3469.  
  3470. /* We really push this many items when pushing a failure point.  We
  3471.    calculate highest_used_reg each time.  */
  3472.  
  3473. #define NUM_FAILURE_ITEMS                        \
  3474.   (highest_used_reg * NUM_REG_ITEMS + NUM_OTHER_ITEMS)
  3475.  
  3476. /* How many items can still be added to the stack without overflowing it.  */
  3477. #define REMAINING_AVAIL_SLOTS                        \
  3478.   (failure_stack.size - failure_stack.avail)
  3479.  
  3480.  
  3481.  
  3482. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  3483.  
  3484. /* Is true if there is a first string and if PTR is pointing anywhere
  3485.    inside it or just past the end.  */
  3486.    
  3487. #define IS_IN_FIRST_STRING(ptr)                     \
  3488.     (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  3489.  
  3490. /* Call before fetching a character with *d.  This switches over to
  3491.    string2 if necessary.  */
  3492.  
  3493. #define PREFETCH                            \
  3494.  while (d == dend)                                \
  3495.   {                                    \
  3496.     /* end of string2 => fail.  */                    \
  3497.     if (dend == end_match_2)                         \
  3498.       goto fail;                            \
  3499.     /* end of string1 => advance to string2.  */             \
  3500.     d = string2;                                \
  3501.     dend = end_match_2;                            \
  3502.   }
  3503.  
  3504.  
  3505.  
  3506. /* Test if at very beginning or at very end of the virtual concatenation
  3507.    of string1 and string2.  If there is only one string, we've put it in
  3508.    string2.  */
  3509.  
  3510. #define AT_STRINGS_BEG  (d == (size1 ? string1 : string2)  ||  !size2)
  3511. #define AT_STRINGS_END  (d == end2)    
  3512.  
  3513. #define AT_WORD_BOUNDARY                        \
  3514.   (AT_STRINGS_BEG || AT_STRINGS_END || IS_A_LETTER (d - 1) != IS_A_LETTER (d))
  3515.  
  3516. /* We have two special cases to check for: 
  3517.      1) if we're past the end of string1, we have to look at the first
  3518.         character in string2;
  3519.      2) if we're before the beginning of string2, we have to look at the
  3520.         last character in string1; we assume there is a string1, so use
  3521.         this in conjunction with AT_STRINGS_BEG.  */
  3522.  
  3523. #define IS_A_LETTER(d)                            \
  3524.   (SYNTAX ((d) == end1 ? *string2 : (d) == string2 - 1 ? *(end1 - 1) : *(d))\
  3525.    == Sword)
  3526.  
  3527. #ifdef REGEX_MALLOC
  3528. #define FREE_VARIABLES                            \
  3529.   do {                                    \
  3530.     free (failure_stack.stack);                        \
  3531.     free (regstart);                            \
  3532.     free (regend);                            \
  3533.     free (old_regstart);                        \
  3534.     free (old_regend);                            \
  3535.     free (reg_info);                            \
  3536.     free (best_regstart);                        \
  3537.     free (best_regend);                            \
  3538.     reg_info = NULL;                            \
  3539.     failure_stack.stack = NULL;                        \
  3540.     regstart = regend = old_regstart = old_regend            \
  3541.       = best_regstart = best_regend = NULL;                \
  3542.   } while (0)
  3543. #endif
  3544.  
  3545.  
  3546.  
  3547. /* The main matching routine, re_match_2.  */
  3548.  
  3549. static void pop_failure_point();
  3550.  
  3551.  
  3552. /* re_match_2 matches a buffer full of byte commands for matching (gotten
  3553.    from compiling a regular expression) and matches it against the
  3554.    the virtual concatenation of its two string arguments.
  3555.    
  3556.    BUFP         is a struct re_pattern_buffer * whose pertinent fields are
  3557.            mentioned below:
  3558.                 
  3559.              It has a char * field BUFFER which points to the byte
  3560.              commands which make up the compiled pattern.
  3561.              
  3562.              Its char * field TRANSLATE, if not 0, translates all
  3563.              ordinary elements in the compiled pattern.
  3564.  
  3565.              Its int field SYNTAX is the syntax with which the pattern
  3566.              was compiled and hence should be matched with.
  3567.              
  3568.              The long field USED is how many bytes long the compiled
  3569.              pattern is.
  3570.              
  3571.              Its size_t field RE_NSUB contains how many subexpressions
  3572.              the pattern has.
  3573.              
  3574.          It ignores its NO_SUB bit.
  3575.              
  3576.              If its RETURN_DEFAULT_NUM_REGS bit is set, then if REGS is
  3577.              nonzero, re_match_2 reports in REGS->start[i] and
  3578.              REGS->end[i], for i = 1 to BUFP->RE_NSUB + 1, which
  3579.              substring of the virtual concatenation of STRING1 and
  3580.              STRING2 matched the i-th subexpression of the regular
  3581.              expression compiled in BUFFER; it records in REGS->start[0]
  3582.              and REGS->end[0] information about all of that
  3583.              concatenation.  If RETURN_DEFAULT_NUM_REGS isn't set,
  3584.              re_match_2 returns in REGS similar information about i
  3585.              things for i = 1 to REGS->num_regs.  If REGS is zero,
  3586.              re_match_2 ignores it.  See the comment for `struct
  3587.              re_registers' for more details.
  3588.  
  3589.    STRING1 and STRING2
  3590.             are the addresses of the strings of which re_match_2 tries
  3591.             to match the virtual concatenation. Because of this
  3592.             concatenation, this function can be used on an Emacs
  3593.             buffer's contents.
  3594.             
  3595.    SIZE1    is the size of STRING1.
  3596.  
  3597.    SIZE2    is the size of STRING2.
  3598.    
  3599.    POS        is the index in the virtual concatenation of STRING1 and
  3600.         STRING2 at which re_match_2 tries to start the match.
  3601.             
  3602.    REGS        is a struct re_registers *.  If it's not zero, then
  3603.             re_match_2 will fill its fields START and END with
  3604.             information about what substrings of the virtual
  3605.             concatenation of STRING1 and STRING2 were matched by the
  3606.             groups represented in BUFP's BUFFER field.  You must have
  3607.             allocated the correct amount of space in the `start' and
  3608.             `end' fields of REGS to accommodate `num_regs' (the other
  3609.             field) registers.  See the comment for `struct re_registers'
  3610.             in regex.h for more details.
  3611.    
  3612.    STOP        is the index in the virtual concatenation of STRING1 and
  3613.         STRING2 beyond which re_match_2 won't consider matching.
  3614.  
  3615.    It returns -1 if there is no match, -2 if there is an internal error
  3616.    (such as its stack overflowing).  Otherwise, it returns the length of
  3617.    the substring it matched.  */
  3618.  
  3619. int
  3620. re_match_2 (bufp, string1_arg, size1_arg, string2_arg, size2_arg, pos, 
  3621.         regs, stop)
  3622.      const struct re_pattern_buffer *bufp;
  3623.      const char *string1_arg;
  3624.      const int size1_arg;
  3625.      const char *string2_arg;
  3626.      const int size2_arg;
  3627.      const int pos;
  3628.      struct re_registers *regs;
  3629.      const int stop;
  3630. {
  3631.   unsigned char *p = (unsigned char *) bufp->buffer;
  3632.   unsigned char *p1;
  3633.  
  3634.   /* Pointer to beyond end of buffer.  */
  3635.   register unsigned char *pend = p + bufp->used;
  3636.  
  3637.   unsigned char *string1 = (unsigned char *) string1_arg;
  3638.   unsigned char *string2 = (unsigned char *) string2_arg;
  3639.   int size1 = size1_arg;
  3640.   int size2 = size2_arg;
  3641.   unsigned char *end1;        /* Just past end of first string.  */
  3642.   unsigned char *end2;        /* Just past end of second string.  */
  3643.  
  3644.  
  3645.   /* Pointers into string1 and string2, just past the last characters in
  3646.      each to consider matching.  */
  3647.   unsigned char *end_match_1, *end_match_2;
  3648.  
  3649.   register unsigned char *d, *dend;
  3650.   int mcnt, mcnt2;                /* Multipurpose.  */
  3651.   unsigned char *translate = (unsigned char *) bufp->translate;
  3652.   unsigned is_a_jump_n = 0;
  3653.  
  3654.   /* This is how many registers the caller wants.  */
  3655.   unsigned num_regs_wanted = regs 
  3656.                         ? bufp->return_default_num_regs
  3657.                                ? bufp->re_nsub + 1
  3658.                                : regs->num_regs
  3659.                              : 0;
  3660.  
  3661.   /* Want to fill *all* the registers internally. */
  3662.   unsigned num_internal_regs = bufp->re_nsub + 1;
  3663.  
  3664.   void *destination;                /* For REGEX_REALLOCATE.  */
  3665.   
  3666.  
  3667.  /* Failure point stack.  Each place that can handle a failure further
  3668.     down the line pushes a failure point on this stack.  It consists of
  3669.     restart, regend, and reg_info for all registers corresponding to the
  3670.     subexpressions we're currently inside, plus the number of such
  3671.     registers, and, finally, two char *'s.  The first char * is where to
  3672.     resume scanning the pattern; the second one is where to resume
  3673.     scanning the strings.  If the latter is zero, the failure point is a
  3674.     ``dummy''; if a failure happens and the failure point is a dummy, it
  3675.     gets discarded and the next next one is tried.  */
  3676.  
  3677.   failure_stack_type failure_stack;
  3678.  
  3679.   /* Information on the contents of registers. These are pointers into
  3680.      the input strings; they record just what was matched (on this
  3681.      attempt) by a subexpression part of the pattern, that is, the
  3682.      regnum-th regstart pointer points to where in the pattern we began
  3683.      matching and the regnum-th regend points to right after where we
  3684.      stopped matching the regnum-th subexpression.  (The zeroth register
  3685.      keeps track of what the whole pattern matches.)  */
  3686.      
  3687.   unsigned char **regstart = (unsigned char **) 
  3688.     REGEX_ALLOCATE (num_internal_regs * sizeof (unsigned char *));
  3689.   unsigned char **regend = (unsigned char **) 
  3690.     REGEX_ALLOCATE (num_internal_regs * sizeof (unsigned char *));
  3691.  
  3692.   /* If a group that's operated upon by a repetition operator fails to
  3693.      match anything, then the register for its start will need to be
  3694.      restored because it will have been set to wherever in the string we
  3695.      are when we last see its open-group operator.  The argument is
  3696.      similar for a register's end.  */
  3697.      
  3698.   unsigned char **old_regstart 
  3699.     = (unsigned char **) REGEX_ALLOCATE (num_internal_regs * sizeof (unsigned char *));
  3700.   unsigned char **old_regend
  3701.     = (unsigned char **) REGEX_ALLOCATE (num_internal_regs * sizeof (unsigned char *));
  3702.  
  3703.   /* The is_active field of reg_info helps us keep track of which (possibly
  3704.      nested) subexpressions we are currently in. The matched_something
  3705.      field of reg_info[reg_num] helps us tell whether or not we have
  3706.      matched any of the pattern so far this time through the reg_num-th
  3707.      subexpression.  These two fields get reset each time through any
  3708.      loop their register is in.  */
  3709.  
  3710.   struct register_info *reg_info = (struct register_info *) 
  3711.      REGEX_ALLOCATE (num_internal_regs * sizeof (struct register_info));
  3712.  
  3713.  
  3714.   /* The following record the register info as found in the above
  3715.      variables when we find a match better than any we've seen before. 
  3716.      This happens as we backtrack through the failure points, which in
  3717.      turn happens only if we have not yet matched the entire string.  */
  3718.  
  3719.   unsigned best_regs_set = 0;
  3720.  
  3721.   unsigned char **best_regstart
  3722.     = (unsigned char **) REGEX_ALLOCATE (num_internal_regs * sizeof (unsigned char *));
  3723.  
  3724.   unsigned char **best_regend
  3725.     = (unsigned char **) REGEX_ALLOCATE (num_internal_regs * sizeof (unsigned char *));
  3726.  
  3727.   unsigned current_reg = 0;
  3728.  
  3729.   /* End of declarations.  */  
  3730.   
  3731.   
  3732.   if (!INIT_FAILURE_STACK (failure_stack))
  3733.     return -2;
  3734.     
  3735.   if (!(regstart && regend && old_regstart && old_regend && reg_info 
  3736.         && best_regstart && best_regend)) 
  3737.     {
  3738. #ifdef REGEX_MALLOC
  3739.       FREE_VARIABLES;
  3740. #endif
  3741.       return -2;
  3742.     }
  3743.  
  3744.   /* The starting position is bogus.  */
  3745.   if (pos < 0 || pos > size1 + size2)
  3746.     {
  3747. #ifdef REGEX_MALLOC
  3748.       FREE_VARIABLES;
  3749. #endif
  3750.       return -1;
  3751.     }
  3752.     
  3753.   
  3754.   /* Initialize subexpression text positions to -1 to mark ones that no
  3755.      \( or ( and \) or ) has been seen for. Also set all registers to
  3756.      inactive and mark them as not having any inner groups, able to
  3757.      match the empty string, matched anything so far, or ever failed.  */
  3758.  
  3759.   for (mcnt = 0; mcnt < num_internal_regs; mcnt++)
  3760.     {
  3761.       regstart[mcnt] = regend[mcnt] 
  3762.         = old_regstart[mcnt] = old_regend[mcnt] = (unsigned char *) -1;
  3763.  
  3764.       if (!INIT_BITS_LIST (INNER_GROUPS (reg_info[mcnt])))
  3765.         {
  3766. #ifdef REGEX_MALLOC
  3767.           FREE_VARIABLES;
  3768. #endif
  3769.           return -2;
  3770.         }
  3771.         
  3772.       CAN_MATCH_NOTHING (reg_info[mcnt]) = -1;        /* I.e., unset.  */
  3773.         /* The bit fields.  */
  3774.       IS_ACTIVE (reg_info[mcnt]) = 0;
  3775.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3776.       EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3777.     }
  3778.   
  3779.   IS_ACTIVE (reg_info[0]) = 1;
  3780.  
  3781.  
  3782.   if (regs && num_regs_wanted > 0)
  3783.     {
  3784.       if (bufp->syntax & RE_ALLOCATE_REGISTERS) 
  3785.     {
  3786.           regs->num_regs = num_regs_wanted;
  3787.           regs->start = (int *) malloc (regs->num_regs * sizeof (int));
  3788.  
  3789.           if (regs->start == NULL)
  3790.             return -2;
  3791.  
  3792.           regs->end = (int *) malloc (regs->num_regs * sizeof (int));
  3793.  
  3794.           if (regs->end == NULL)
  3795.             return -2;
  3796.         }
  3797.   
  3798.     for (mcnt = 0; mcnt < regs->num_regs; mcnt++)
  3799.       {
  3800.         regs->start[mcnt] = -1;
  3801.         regs->end[mcnt] = -1;
  3802.       }
  3803.   }
  3804.  
  3805.   
  3806.  
  3807.   /* Set up pointers to ends of strings.
  3808.      Don't allow the second string to be empty unless both are empty.  */
  3809.   if (size2 == 0)
  3810.     {
  3811.       string2 = string1;
  3812.       size2 = size1;
  3813.       string1 = 0;
  3814.       size1 = 0;
  3815.     }
  3816.   end1 = string1 + size1;
  3817.   end2 = string2 + size2;
  3818.  
  3819.  
  3820.   /* Compute where to stop matching, within the two strings.  */
  3821.   if (stop <= size1)
  3822.     {
  3823.       end_match_1 = string1 + stop;
  3824.       end_match_2 = string2;
  3825.     }
  3826.   else
  3827.     {
  3828.       end_match_1 = end1;
  3829.       end_match_2 = string2 + stop - size1;
  3830.     }
  3831.  
  3832.   /* `p' scans through the pattern as `d' scans through the data. `dend'
  3833.      is the end of the input string that `d' points within. `d' is
  3834.      advanced into the following input string whenever necessary, but
  3835.      this happens before fetching; therefore, at the beginning of the
  3836.      loop, `d' can be pointing at the end of a string, but it cannot
  3837.      equal `string2'.  */
  3838.  
  3839.   if (size1 != 0 && pos <= size1)
  3840.     {
  3841.       d = string1 + pos;
  3842.       dend = end_match_1;
  3843.     }
  3844.   else
  3845.     {
  3846.       d = string2 + pos - size1;
  3847.       dend = end_match_2;
  3848.     }
  3849.  
  3850.  
  3851.   /* This loops over pattern commands.  It exits by returning from the
  3852.      function if match is complete, or it drops through if match fails
  3853.      at this starting point in the input data.  */
  3854.  
  3855.   while (1)
  3856.     {
  3857.       is_a_jump_n = 0;
  3858.       /* End of pattern means we might have succeeded.  */
  3859.       if (p == pend)
  3860.     {
  3861.       /* If not end of string, try backtracking.  Otherwise done.  */
  3862.           if (d != end_match_2)
  3863.         {
  3864.               if (!FAILURE_STACK_EMPTY)
  3865.                 {
  3866.                   /* More failure points to try.  */
  3867.  
  3868.                   unsigned in_same_string = 
  3869.                           IS_IN_FIRST_STRING (best_regend[0]) 
  3870.                         == MATCHING_IN_FIRST_STRING;
  3871.  
  3872.                   /* If exceeds best match so far, save it.  */
  3873.                   if (! best_regs_set
  3874.                       || (in_same_string && d > best_regend[0])
  3875.                       || (! in_same_string && ! MATCHING_IN_FIRST_STRING))
  3876.                     {
  3877.                       best_regs_set = 1;
  3878.                       best_regend[0] = d;    /* Never use regstart[0].  */
  3879.                       
  3880.                       for (mcnt = 1; mcnt < num_internal_regs; mcnt++)
  3881.                         {
  3882.                           best_regstart[mcnt] = regstart[mcnt];
  3883.                           best_regend[mcnt] = regend[mcnt];
  3884.                         }
  3885.                     }
  3886.                   goto fail;           
  3887.                 }
  3888.               /* If no failure points, don't restore garbage.  */
  3889.               else if (best_regs_set)   
  3890.                 {
  3891.           restore_best_regs:
  3892.                   /* Restore best match.  */
  3893.                   d = best_regend[0];
  3894.                   
  3895.                   if (d >= string1 && d <= end1)
  3896.                     dend = end_match_1;
  3897.  
  3898.           for (mcnt = 0; mcnt < num_internal_regs; mcnt++)
  3899.             {
  3900.               regstart[mcnt] = best_regstart[mcnt];
  3901.               regend[mcnt] = best_regend[mcnt];
  3902.             }
  3903.                 }
  3904.             } /* if (d != end_match_2) */
  3905.  
  3906.       /* If caller wants register contents data back, convert it 
  3907.          to indices.  */
  3908.       if (regs && regs->num_regs > 0)
  3909.         {
  3910.           regs->start[0] = pos;
  3911.  
  3912.               regs->end[0] = MATCHING_IN_FIRST_STRING
  3913.                          ? d - string1
  3914.                  : d - string2 + size1;
  3915.                 
  3916.           for (mcnt = 1; mcnt < regs->num_regs; mcnt++)
  3917.         {
  3918.                   if (mcnt >= num_internal_regs
  3919.                       || regstart[mcnt] == (unsigned char *) -1 
  3920.                       || regend[mcnt] == (unsigned char *) -1)
  3921.             {
  3922.               regs->start[mcnt] = -1;
  3923.               regs->end[mcnt] = -1;
  3924.               continue;
  3925.             }
  3926.           
  3927.                   regs->start[mcnt] = IS_IN_FIRST_STRING (regstart[mcnt])
  3928.                       ? regstart[mcnt] - string1
  3929.                                       : regstart[mcnt] - string2 + size1;
  3930.                     
  3931.                   regs->end[mcnt] = IS_IN_FIRST_STRING (regend[mcnt])
  3932.                             ? regend[mcnt] - string1
  3933.                                     : regend[mcnt] - string2 + size1;
  3934.         }
  3935.         }
  3936.  
  3937. #ifdef REGEX_MALLOC
  3938.           FREE_VARIABLES;
  3939. #endif
  3940.           return d - pos - (MATCHING_IN_FIRST_STRING 
  3941.                 ? string1 
  3942.                 : string2 - size1);
  3943.         }
  3944.  
  3945.       /* Otherwise match next pattern command.  */
  3946. #ifdef SWITCH_ENUM_BUG
  3947.       switch ((int) ((enum regexpcode) *p++))
  3948. #else
  3949.       switch ((enum regexpcode) *p++)
  3950. #endif
  3951.     {
  3952.  
  3953.     /* \( [or `(', as appropriate] is represented by start_memory,
  3954.            \) by stop_memory.  Both of those commands are followed by a
  3955.            register number in the next byte.  The text matched within
  3956.            the \( and \) is recorded (in the internal registers data
  3957.            structure) under that number.  */
  3958.  
  3959.         case start_memory:
  3960.           /* Find out if this group can match the empty string.  */
  3961.       p1 = p;        /* To send to group_can_match_nothing.  */
  3962.           
  3963.           if (CAN_MATCH_NOTHING (reg_info[*p]) == -1)
  3964.             CAN_MATCH_NOTHING (reg_info[*p]) 
  3965.               = group_can_match_nothing (&p1, pend, reg_info);
  3966.  
  3967.           /* Save the position in the string where we were the last time
  3968.              we were at this open-group operator in case the group is
  3969.              operated upon by a repetition operator, e.g., with `(a*)*b'
  3970.              against `ab'; then we want to ignore where we are now in
  3971.              the string in case this attempt to match fails.  */
  3972.  
  3973.           old_regstart[*p] = CAN_MATCH_NOTHING (reg_info[*p])
  3974.                              ? ((regstart[*p] == (unsigned char *) -1)
  3975.                                  ? d : regstart[*p])
  3976.                              : regstart[*p];
  3977.           regstart[*p] = d;
  3978.  
  3979.           IS_ACTIVE (reg_info[*p]) = 1;
  3980.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  3981.           p++;
  3982.           break;
  3983.  
  3984.     case stop_memory:
  3985.           /* Save the position we were in the string the last time we
  3986.              were at this close-group operator in case the group is
  3987.              operated upon by a repetition operator, e.g., with 
  3988.              `((a*)*(b*)*)*' against `aba'; then we want to ignore where
  3989.              we are now in the string in case this attempt to match
  3990.              fails.  */
  3991.              
  3992.           old_regend[*p] = CAN_MATCH_NOTHING (reg_info[*p])
  3993.                            ? ((regend[*p] == (unsigned char *) -1)
  3994.                                ? d : regend[*p])
  3995.                : regend[*p];
  3996.           regend[*p] = d;
  3997.           IS_ACTIVE (reg_info[*p]) = 0;
  3998.       
  3999.       /* Record that this group is inside of all currently active
  4000.              groups; makes no sense for group 1.  */
  4001.           if (*p != 1)
  4002.             NOTE_INNER_GROUP (*p);
  4003.             
  4004.           
  4005.           /* If just failed to match something this time around with a
  4006.              group that's operated on by a repetition operator, try to
  4007.              force exit from the ``loop,'' and restore the register
  4008.              information for this group that we had before trying this
  4009.              last match.  */
  4010.              
  4011.           if ((!MATCHED_SOMETHING (reg_info[*p])
  4012.                || (enum regexpcode) p[-3] == start_memory)
  4013.           && (p + 1) != pend)              
  4014.             {
  4015.               p1 = p + 1;
  4016.               mcnt = 0;
  4017.               switch ((enum regexpcode) *p1++)
  4018.                 {
  4019.                   case no_pop_jump_n:
  4020.             is_a_jump_n = 1;
  4021.                   case pop_failure_jump:
  4022.           case maybe_pop_jump:
  4023.           case no_pop_jump:
  4024.           case dummy_failure_jump:
  4025.                     extract_number_and_incr (&mcnt, &p1);
  4026.             if (is_a_jump_n)
  4027.               p1 += 2;
  4028.                     break;
  4029.                 }
  4030.           p1 += mcnt;
  4031.         
  4032.               /* If the next operation is a jump backwards in the pattern
  4033.              to an on_failure_jump right before the start_memory
  4034.                  corresponding to this stop_memory, exit from the loop
  4035.                  by forcing a failure after pushing on the stack the
  4036.                  on_failure_jump's jump in the pattern, and d.  */
  4037.  
  4038.               if (mcnt < 0 && (enum regexpcode) *p1 == on_failure_jump
  4039.                   && (enum regexpcode) p1[3] == start_memory && p1[4] == *p)
  4040.         {
  4041.                   /* If this group ever matched anything, then
  4042.              restore what its registers were before trying
  4043.                      this last failed match, e.g., with `(a*)*b' against
  4044.                      `ab' for regstart[1], and, e.g., with `((a*)*(b*)*)*'
  4045.                      against `aba' for regend[3]. 
  4046.                      
  4047.                      Restore the registers for inner groups, too, e.g.,
  4048.                      for `((a*)(b*))*' against `aba' (register 2 gets
  4049.                      trashed).  */
  4050.                      
  4051.                   if (EVER_MATCHED_SOMETHING (reg_info[*p]))
  4052.             {
  4053.               unsigned this_reg; 
  4054.                   unsigned bits_mask;
  4055.         
  4056.                       EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
  4057.                       
  4058.               /* Restore this group's registers.  */
  4059.                       
  4060.                         regstart[*p] = old_regstart[*p];
  4061.                         regend[*p] = old_regend[*p];
  4062.  
  4063.               /* Restore the inner groups' (if any) registers.  */
  4064.  
  4065.                       for (this_reg = 0;
  4066.                            this_reg < INNER_GROUPS (reg_info[*p]).size;
  4067.                            this_reg++)
  4068.                         {
  4069.                           if (get_bit (INNER_GROUPS (reg_info[*p]), this_reg))
  4070.                             {
  4071.                               regstart[this_reg] = old_regstart[this_reg];
  4072.  
  4073.                               if ((int)old_regend[this_reg] 
  4074.                                    >= (int)regstart[this_reg])
  4075.                                 regend[this_reg] = old_regend[this_reg];
  4076.                             }
  4077.                         }     
  4078.                     }
  4079.           p1++;
  4080.                   extract_number_and_incr (&mcnt, &p1);
  4081.                   PUSH_FAILURE_POINT (p1 + mcnt, d, failure_stack, -2);
  4082.  
  4083.                   goto fail;
  4084.                 }
  4085.             }
  4086.           p++;
  4087.           break;
  4088.  
  4089.     /* \<digit> has been turned into a `duplicate' command which is
  4090.            followed by the numeric value of <digit> as the register number.  */
  4091.         case duplicate:
  4092.       {
  4093.         register unsigned char *d2, *dend2;
  4094.         int regno = *p++;   /* Get which register to match against.  */
  4095.  
  4096.         /* Can't back reference a group which we've never matched.  */
  4097.             if ((regstart[regno] == (unsigned char *) -1
  4098.              || regend[regno] == (unsigned char *) -1)
  4099.                 && ! bufp->can_be_null)
  4100.               goto really_fail;
  4101.               
  4102.             /* Where in input to try to start matching.  */
  4103.             d2 = regstart[regno];
  4104.             
  4105.             /* Where to stop matching; if both the place to start and
  4106.                the place to stop matching are in the same string, then
  4107.                set to the place to stop, otherwise, for now have to use
  4108.                the end of the first string.  */
  4109.  
  4110.             dend2 = ((IS_IN_FIRST_STRING (regstart[regno]) 
  4111.               == IS_IN_FIRST_STRING (regend[regno]))
  4112.              ? regend[regno] : end_match_1);
  4113.         while (1)
  4114.           {
  4115.         /* If necessary, advance to next segment in register
  4116.                    contents.  */
  4117.         while (d2 == dend2)
  4118.           {
  4119.             if (dend2 == end_match_2) break;
  4120.             if (dend2 == regend[regno]) break;
  4121.  
  4122.                     /* end of string1 => advance to string2. */
  4123.                     d2 = string2;
  4124.                     dend2 = regend[regno];
  4125.           }
  4126.         /* At end of register contents => success */
  4127.         if (d2 == dend2) break;
  4128.  
  4129.         /* If necessary, advance to next segment in data.  */
  4130.         PREFETCH;
  4131.  
  4132.         /* How many characters left in this segment to match.  */
  4133.         mcnt = dend - d;
  4134.                 
  4135.         /* Want how many consecutive characters we can match in
  4136.                    one shot, so, if necessary, adjust the count.  */
  4137.                 if (mcnt > dend2 - d2)
  4138.           mcnt = dend2 - d2;
  4139.                   
  4140.         /* Compare that many; failure if mismatch, else move
  4141.                    past them.  */
  4142.         if (translate 
  4143.                     ? bcmp_translate (d, d2, mcnt, translate) 
  4144.                     : bcmp (d, d2, mcnt))
  4145.           goto fail;
  4146.         d += mcnt, d2 += mcnt;
  4147.           }
  4148.       }
  4149.       break;
  4150.  
  4151.     case anychar:
  4152.       PREFETCH;      /* Fetch a data character. */
  4153.       /* Match anything but possibly a newline or a null.  */
  4154.           if ((!(bufp->syntax & RE_DOT_NEWLINE)
  4155.                && (translate ? translate[*d] : *d) == '\n')
  4156.               || ((bufp->syntax & RE_DOT_NOT_NULL) 
  4157.                   && (translate ? translate[*d] : *d) == '\000'))
  4158.         goto fail;
  4159.  
  4160.           SET_REGS_MATCHED;
  4161.           d++;
  4162.       break;
  4163.  
  4164.     case charset:
  4165.     case charset_not:
  4166.       {
  4167.         int not = 0;        /* Nonzero for charset_not.  */
  4168.         register int c;
  4169.         if (*(p - 1) == (unsigned char) charset_not)
  4170.           not = 1;
  4171.  
  4172.         PREFETCH;        /* Fetch a data character. */
  4173.  
  4174.         c = translate ? translate[*d] : *d;
  4175.  
  4176.         if (c < *p * BYTEWIDTH
  4177.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  4178.           not = !not;
  4179.  
  4180.         p += 1 + *p;
  4181.  
  4182.         if (!not) goto fail;
  4183.         SET_REGS_MATCHED;
  4184.             d++;
  4185.         break;
  4186.       }
  4187.  
  4188.     case begline:
  4189.       if (bufp->not_bol == 1)
  4190.             goto fail;
  4191.  
  4192.           if (d && (*d == '\n' || d[-1] == '\n')) 
  4193.             {
  4194.           if (*d == '\n') 
  4195.                 d++;
  4196.               
  4197.               if (bufp->syntax & RE_NO_ANCHOR_AT_NEWLINE)
  4198.                 goto fail;
  4199.               else
  4200.                 break;
  4201.             }
  4202.           
  4203.           if ((size1 != 0 && d == string1)
  4204.               || (size1 == 0 && size2 != 0 && d == string2)
  4205.               || (size1 == 0 && size2 == 0))
  4206.             break;
  4207.           else
  4208.             goto fail;
  4209.             
  4210.     case endline:
  4211.       if (bufp->not_eol == 1)
  4212.             goto fail;
  4213.             
  4214.       if (d == end2
  4215.           || (d == end1  && size2 == 0))
  4216.         break;
  4217.  
  4218.           if (*d == '\n' || (d == end1 && *string2 == '\n'))
  4219.             {
  4220.           PREFETCH;
  4221.  
  4222.               if (*d == '\n')
  4223.                 d++;
  4224.  
  4225.               if (bufp->syntax & RE_NO_ANCHOR_AT_NEWLINE)
  4226.                 goto fail;
  4227.               else
  4228.                 break;
  4229.             }
  4230.       goto fail;
  4231.  
  4232.     /*  Uses of on_failure_jump:
  4233.         
  4234.            Each alternative starts with an on_failure_jump that points
  4235.            to the beginning of the next alternative.  Each alternative
  4236.            except the last ends with a jump that in effect jumps past
  4237.            the rest of the alternatives.  (They really jump to the
  4238.            ending jump of the following alternative, because tensioning
  4239.            these jumps is a hassle.)
  4240.  
  4241.            Repeats start with an on_failure_jump that points past both
  4242.            the repetition text and the following jump or
  4243.            pop_failure_jump back to this on_failure_jump.  */
  4244.  
  4245.     case on_failure_jump:
  4246.         on_failure:
  4247.           extract_number_and_incr (&mcnt, &p);
  4248.           PUSH_FAILURE_POINT (p + mcnt, d, failure_stack, -2);
  4249.  
  4250.           break;
  4251.  
  4252.  
  4253.         /* A smart repeat ends with a maybe_pop_jump.
  4254.        We change it either to a pop_failure_jump or a no_pop_jump.  */
  4255.  
  4256.         case maybe_pop_jump:
  4257.           extract_number_and_incr (&mcnt, &p);
  4258.       {
  4259.         register unsigned char *p2 = p;
  4260.  
  4261.             /* Compare the beginning of the repeat with what in the
  4262.                pattern follows its end. If we can establish that there
  4263.                is nothing that they would both match, i.e., that we
  4264.                would have to backtrack because of (as would in, e.g.,
  4265.                `a*a') then we can change to pop_failure_jump, because
  4266.                we'll never have to backtrack.  */
  4267.  
  4268.         /* Skip over parentheses.  */
  4269.         while (p2 + 1 != pend
  4270.            && (*p2 == (unsigned char) stop_memory
  4271.                || *p2 == (unsigned char) start_memory))
  4272.           p2 += 2;            /* Skip over reg number, too.  */
  4273.  
  4274.             if (p2 == pend)
  4275.           p[-3] = (unsigned char) pop_failure_jump;
  4276.             else if (*p2 == (unsigned char) exactn
  4277.              || *p2 == (unsigned char) endline)
  4278.           {
  4279.         register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  4280.         register unsigned char *p1 = p + mcnt;
  4281.  
  4282.                 /* p1[0] ... p1[2] are the on_failure_jump corresponding
  4283.                    to the maybe_finalize_jump of this case. Examine what 
  4284.                    follows it.  */
  4285.  
  4286.                 if (p1[3] == (unsigned char) exactn && p1[5] != c)
  4287.           p[-3] = (unsigned char) pop_failure_jump;
  4288.         else if (p1[3] == (unsigned char) charset
  4289.              || p1[3] == (unsigned char) charset_not)
  4290.           {
  4291.             int not = p1[3] == (unsigned char) charset_not;
  4292.             if (c < p1[4] * BYTEWIDTH
  4293.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  4294.               not = !not;
  4295.             /* `not' is equal to 1 if c would match, which means
  4296.                         that we can't change to pop_failure_jump.  */
  4297.             if (!not)
  4298.               p[-3] = (unsigned char) pop_failure_jump;
  4299.           }
  4300.           }
  4301.       }
  4302.       p -= 2;        /* Point at relative address again.  */
  4303.       if (p[-1] != (unsigned char) pop_failure_jump)
  4304.         {
  4305.           p[-1] = (unsigned char) no_pop_jump;    
  4306.           goto no_pop;
  4307.         }
  4308.         /* Note fall through.  */
  4309.  
  4310.     /* The end of a simple repeat has a pop_failure_jump back to
  4311.            its matching on_failure_jump, where the latter will push a
  4312.            failure point point.  The pop_failure_jump takes off failure
  4313.            points put on by this pop_failure_jump's matching
  4314.            on_failure_jump; we got through the pattern to here from the
  4315.            matching on_failure_jump, so didn't fail.  Also remove the
  4316.            register information put on by the matching on_failure_jump.  */
  4317.            
  4318.         case pop_failure_jump:
  4319.         pop:
  4320.           pop_failure_point (&failure_stack);
  4321.           /* Note fall through.  */
  4322.           
  4323.         /* Jump without taking off any failure points.  */
  4324.  
  4325.         case no_pop_jump:
  4326.     no_pop:
  4327.       extract_number_and_incr (&mcnt, &p);    /* Get the amount to jump.  */
  4328.       p += mcnt;                /* Do the jump.  */
  4329.       break;
  4330.  
  4331.     
  4332.         /* If the last alternative didn't match anything and empty
  4333.            alternatives aren't allowed, then don't skip over the next
  4334.            one.  */
  4335.    
  4336.         case jump_past_next_alt:
  4337.       {
  4338.             int this_reg;    /* Counting down.  */
  4339.  
  4340.             /* The current register is the innermost (the one with the
  4341.                highest number) active one.  */
  4342.  
  4343.               for (this_reg = num_internal_regs - 1; 
  4344.                    this_reg >= 0; this_reg--)    
  4345.                 if (IS_ACTIVE (reg_info[this_reg]))
  4346.                     break;
  4347.  
  4348.             if (!(bufp->syntax & RE_NO_EMPTY_ALTS)
  4349.                 || MATCHED_SOMETHING (reg_info[this_reg]))
  4350.               goto no_pop;
  4351.  
  4352.             p += 2;            /* Skip past the jump's number.  */
  4353.             break;
  4354.       }
  4355.  
  4356.         case dummy_failure_jump:
  4357.           /* Normally, the on_failure_jump pushes a failure point, which
  4358.              then gets popped at pop_failure_jump.  We will end up at
  4359.              pop_failure_jump, also, and with a pattern of, say, `a+', we
  4360.              are skipping over the on_failure_jump, so we have to push
  4361.              something meaningless for pop_failure_jump to pop.  */
  4362.  
  4363.           PUSH_FAILURE_POINT (0, 0, failure_stack, -2);
  4364.             
  4365.           goto no_pop;
  4366.  
  4367.  
  4368.         /* Have to succeed matching what follows at least n times.  Then
  4369.           just handle like an on_failure_jump.  */
  4370.         case succeed_n: 
  4371.           mcnt = extract_number (p + 2);
  4372.           /* Originally, this is how many times we HAVE to succeed.  */
  4373.           if (mcnt)
  4374.             {
  4375.                mcnt--;
  4376.            p += 2;
  4377.                STORE_NUMBER_AND_INCR (p, mcnt);
  4378.             }
  4379.       else if (mcnt == 0)
  4380.             {
  4381.           p[2] = (char) no_op;
  4382.               p[3] = (char) no_op;
  4383.               goto on_failure;
  4384.             }
  4385.           else
  4386.         { 
  4387.               fprintf (stderr, "regex: the succeed_n's n is not set.\n");
  4388.               exit (1);
  4389.         }
  4390.           break;
  4391.         
  4392.         case no_pop_jump_n: 
  4393.           mcnt = extract_number (p + 2);
  4394.           /* Originally, this is how many times we CAN jump.  */
  4395.           if (mcnt)
  4396.             {
  4397.                mcnt--;
  4398.                STORE_NUMBER(p + 2, mcnt);
  4399.            goto no_pop;         
  4400.             }
  4401.           /* If don't have to jump any more, skip over the rest of command.  */
  4402.       else      
  4403.         p += 4;             
  4404.           break;
  4405.         
  4406.     case set_number_at:
  4407.       {
  4408.           register unsigned char *p1;
  4409.  
  4410.             extract_number_and_incr (&mcnt, &p);
  4411.             p1 = p + mcnt;
  4412.             extract_number_and_incr (&mcnt, &p);
  4413.         STORE_NUMBER (p1, mcnt);
  4414.             break;
  4415.           }
  4416.  
  4417.         /* Ignore these.  Used to ignore the n of succeed_n's which
  4418.            currently have n == 0.  */
  4419.         case no_op:
  4420.           break;
  4421.  
  4422.         case wordbound:
  4423.       if (AT_WORD_BOUNDARY)
  4424.         break;
  4425.       goto fail;
  4426.  
  4427.     case notwordbound:
  4428.       if (AT_WORD_BOUNDARY)
  4429.         goto fail;
  4430.       break;
  4431.  
  4432.     case wordbeg:
  4433.           /* Have to check if AT_STRINGS_BEG before looking at d - 1.  */
  4434.       if (IS_A_LETTER (d) && (AT_STRINGS_BEG || !IS_A_LETTER (d - 1)))
  4435.         break;
  4436.       goto fail;
  4437.  
  4438.     case wordend:
  4439.           /* Have to check if AT_STRINGS_BEG before looking at d - 1.  */
  4440.       if (!AT_STRINGS_BEG && IS_A_LETTER (d - 1)
  4441.               && (!IS_A_LETTER (d) || AT_STRINGS_END))
  4442.         break;
  4443.       goto fail;
  4444.  
  4445. #ifdef emacs
  4446. #ifdef emacs19
  4447.       case before_dot:
  4448.        if (PTR_CHAR_POS (d) >= point)
  4449.           goto fail;
  4450.         break;
  4451.   
  4452.       case at_dot:
  4453.        if (PTR_CHAR_POS (d) != point)
  4454.           goto fail;
  4455.         break;
  4456.   
  4457.       case after_dot:
  4458.        if (PTR_CHAR_POS (d) <= point)
  4459.           goto fail;
  4460.         break;
  4461. #else /* not emacs19 */
  4462.         case before_dot:
  4463.       if (((d - string2 <= (unsigned) size2)
  4464.            ? d - bf_p2 : d - bf_p1)
  4465.           <= point)
  4466.         goto fail;
  4467.       break;
  4468.  
  4469.     case at_dot:
  4470.       if (((d - string2 <= (unsigned) size2)
  4471.            ? d - bf_p2 : d - bf_p1)
  4472.           == point)
  4473.         goto fail;
  4474.       break;
  4475.  
  4476.     case after_dot:
  4477.       if (((d - string2 <= (unsigned) size2)
  4478.            ? d - bf_p2 : d - bf_p1)
  4479.           >= point)
  4480.         goto fail;
  4481.       break;
  4482. #endif /* not emacs19 */
  4483.  
  4484.     case wordchar:
  4485.       mcnt = (int) Sword;
  4486.       goto matchsyntax;
  4487.  
  4488.     case syntaxspec:
  4489.       mcnt = *p++;
  4490.     matchsyntax:
  4491.       PREFETCH;
  4492.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  4493.           SET_REGS_MATCHED;
  4494.       break;
  4495.       
  4496.     case notwordchar:
  4497.       mcnt = (int) Sword;
  4498.       goto matchnotsyntax;
  4499.  
  4500.     case notsyntaxspec:
  4501.       mcnt = *p++;
  4502.     matchnotsyntax:
  4503.       PREFETCH;
  4504.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  4505.       SET_REGS_MATCHED;
  4506.           break;
  4507.  
  4508. #else /* not emacs */
  4509.  
  4510.     case wordchar:
  4511.       PREFETCH;
  4512.           if (!IS_A_LETTER (d))
  4513.             goto fail;
  4514.       SET_REGS_MATCHED;
  4515.       break;
  4516.       
  4517.     case notwordchar:
  4518.       PREFETCH;
  4519.       if (IS_A_LETTER (d))
  4520.             goto fail;
  4521.           SET_REGS_MATCHED;
  4522.       break;
  4523.  
  4524. #endif /* not emacs */
  4525.  
  4526.     case begbuf:
  4527.           if (AT_STRINGS_BEG)
  4528.             break;
  4529.           goto fail;
  4530.  
  4531.         case endbuf:
  4532.       if (AT_STRINGS_END)
  4533.         break;
  4534.       goto fail;
  4535.  
  4536.     case exactn:
  4537.       /* Match the next few pattern characters exactly.
  4538.          mcnt is how many characters to match.  */
  4539.       mcnt = *p++;
  4540.       /* This is written out as an if-else so we don't waste time
  4541.              testing `translate' inside the loop.  */
  4542.           if (translate)
  4543.         {
  4544.           do
  4545.         {
  4546.           PREFETCH;
  4547.           if (translate[*d++] != *p++) goto fail;
  4548.         }
  4549.           while (--mcnt);
  4550.         }
  4551.       else
  4552.         {
  4553.           do
  4554.         {
  4555.           PREFETCH;
  4556.           if (*d++ != *p++) goto fail;
  4557.         }
  4558.           while (--mcnt);
  4559.         }
  4560.       SET_REGS_MATCHED;
  4561.           break;
  4562.     }
  4563.       continue;  /* Successfully executed one pattern command; keep going.  */
  4564.  
  4565.     /* Jump here if any matching operation fails. */
  4566.     fail:
  4567.       if (!FAILURE_STACK_EMPTY)
  4568.     /* A restart point is known.  Restart there and pop it. */
  4569.     {
  4570.           short highest_used_reg, this_reg;
  4571.           boolean is_a_jump_n = false;
  4572.  
  4573.           /* If this failure point is from a dummy_failure_point,
  4574.              just skip it.  */
  4575.              
  4576.           if (!failure_stack.stack[failure_stack.avail - 2])
  4577.             {
  4578.           pop_failure_point (&failure_stack);
  4579.               goto fail;
  4580.             }
  4581.  
  4582.           /* Among other things, undo the last failure point push.  */
  4583.  
  4584.           d = failure_stack.stack[--failure_stack.avail];
  4585.           p = failure_stack.stack[--failure_stack.avail];
  4586.  
  4587.  
  4588.           /* If failed to a backwards jump that's part of a repetition
  4589.              loop, need to pop this failure point and use the next one.  */
  4590.  
  4591.           switch ((enum regexpcode) *p)
  4592.             {
  4593.             case no_pop_jump_n:
  4594.           is_a_jump_n = true;
  4595.             case maybe_pop_jump:
  4596.             case pop_failure_jump:
  4597.             case no_pop_jump:
  4598.               p1 = p + 1;
  4599.               extract_number_and_incr (&mcnt, &p1);
  4600.           p1 += mcnt;    
  4601.  
  4602.               if ((is_a_jump_n && *p1 == succeed_n)
  4603.                     || (!is_a_jump_n && *p1 == on_failure_jump))
  4604.             {
  4605.           /* Put p and d back on the stack again...  */
  4606.                   failure_stack.avail += 2;           
  4607.                   
  4608.                   /* ...and pop the whole failure point.  */
  4609.             pop_failure_point (&failure_stack);
  4610.           goto fail;
  4611.                 }
  4612.               break;
  4613.             }
  4614.  
  4615.           if (d >= string1 && d <= end1)
  4616.         dend = end_match_1;
  4617.  
  4618.           /* Restore register info.  */
  4619.           highest_used_reg 
  4620.             = (short) failure_stack.stack[--failure_stack.avail];
  4621.           
  4622.           /* Make the ones that weren't saved -1 or 0 again.  */
  4623.           for (this_reg = num_internal_regs - 1; this_reg > highest_used_reg; 
  4624.            this_reg--)
  4625.             {
  4626.               regend[this_reg] = (unsigned char *) -1;
  4627.               regstart[this_reg] = (unsigned char *) -1;
  4628.               IS_ACTIVE (reg_info[this_reg]) = 0;
  4629.               MATCHED_SOMETHING (reg_info[this_reg]) = 0;
  4630.             }
  4631.           
  4632.           /* And restore the rest from the stack.  */
  4633.           for ( ; this_reg > 0; this_reg--)
  4634.             {
  4635.               reg_info[this_reg] = *(struct register_info *) 
  4636.                 failure_stack.stack[--failure_stack.avail];
  4637.  
  4638.               regend[this_reg] 
  4639.                 = failure_stack.stack[--failure_stack.avail];
  4640.   
  4641.               regstart[this_reg] 
  4642.                 = failure_stack.stack[--failure_stack.avail];
  4643.             }
  4644.         }
  4645.       else
  4646.         break;   /* Matching at this starting point really fails.  */
  4647.     } /* while (1) */
  4648.  
  4649.   really_fail:
  4650.   if (best_regs_set)
  4651.     goto restore_best_regs;
  4652.  
  4653. #ifdef REGEX_MALLOC
  4654.   FREE_VARIABLES;
  4655. #endif
  4656.   return -1;                     /* Failure to match.  */
  4657. }
  4658.  
  4659.  
  4660.  
  4661.  
  4662. /* Subroutine definitions for re_match_2.  */
  4663.  
  4664.  
  4665.  
  4666. /* Failure stack stuff.  */
  4667.  
  4668. /* Pops what PUSH_FAILURE_STACK pushes.  */
  4669.  
  4670. static void 
  4671. pop_failure_point(failure_stack_ptr)
  4672.   failure_stack_type *failure_stack_ptr;
  4673. {                                    
  4674.   int temp;                                
  4675.  
  4676.   if (FAILURE_STACK_PTR_EMPTY)
  4677.     {                                    
  4678.       printf ("Tried to pop empty failure point in re_match_2.\n");    
  4679.       exit (1);                            
  4680.     }
  4681.  
  4682.   /* Remove failure points and point to how many regs pushed.  */
  4683.   else
  4684.     {
  4685.       if (failure_stack_ptr->avail < 3)
  4686.         {
  4687.           printf ("Aren't enough items to pop on re_match_2 failure stack: \
  4688. there's only %d on it.\n", failure_stack_ptr->avail);
  4689.           exit (1);
  4690.         }
  4691.       failure_stack_ptr->avail -= 3;
  4692.       temp = (int) failure_stack_ptr->stack[failure_stack_ptr->avail];
  4693.       temp *= NUM_REG_ITEMS;        /* How much to take off the stack.  */
  4694.       
  4695.       if (failure_stack_ptr->avail < temp)
  4696.         {
  4697.           printf ("Can't pop %d items off re_match_2 failure stack: \
  4698. there's only %d on it.\n", temp, failure_stack_ptr->avail);
  4699.           exit (1);
  4700.         }
  4701.       failure_stack_ptr->avail -= temp;    /* Remove the register info.  */
  4702.     }
  4703. }
  4704.  
  4705.  
  4706. /* Other things.  */
  4707.  
  4708. static boolean common_op_can_match_nothing ();
  4709. static boolean alternative_can_match_nothing ();
  4710.  
  4711.  
  4712. /* We are given P pointing to a register number after a start_memory.
  4713.    
  4714.    Return true if the pattern up to the corresponding stop_memory can
  4715.    match the empty string, and false otherwise.
  4716.    
  4717.    If we find the matching stop_memory, sets P to point to one past its number.
  4718.    Otherwise, sets P to an undefined byte less than or equal to END.
  4719.  
  4720.    We don't handle duplicates properly (yet).  */
  4721.  
  4722. static boolean
  4723. group_can_match_nothing (p, end, reg_info)
  4724.   unsigned char **p, *end;
  4725.   struct register_info *reg_info;
  4726. {
  4727.   int mcnt;
  4728.   unsigned char *p1 = *p + 1;      /* Point to after this register number.  */
  4729.   
  4730.   while (p1 < end)
  4731.     {
  4732.       /* Skip over opcodes that can match nothing, and return true or
  4733.      false, as appropriate, when we get to one that can't, or to the
  4734.          matching stop_memory.  */
  4735.       
  4736.       switch ((enum regexpcode) *p1)
  4737.         {
  4738.         /* Could be either a loop or a series of alternatives.  */
  4739.         case on_failure_jump:
  4740.           p1++;
  4741.           extract_number_and_incr (&mcnt, &p1);
  4742.           
  4743.           /* If the next operation is not a jump backwards in the
  4744.          pattern.  */
  4745.  
  4746.       if (mcnt >= 0)
  4747.         {
  4748.               /* Go through the on_failure_jumps of the alternatives,
  4749.                  seeing if any of the alternatives cannot match nothing.
  4750.                  The last alternative starts with only a no_pop_jump,
  4751.                  whereas the rest start with on_failure_jump and end
  4752.                  with a no_pop_jump, e.g., here is the pattern for `a|b|c':
  4753.  
  4754.                  /on_failure_jump/0/6/exactn/1/a/jump_past_next_alt/0/6
  4755.                  /on_failure_jump/0/6/exactn/1/b/jump_past_next_alt/0/3
  4756.                  /exactn/1/c                        
  4757.  
  4758.                  So, we have to first go through the first (n-1)
  4759.                  alternatives and then deal with the last one separately.  */
  4760.  
  4761.  
  4762.               /* Deal with the first (n-1) alternatives, which start
  4763.                  with an on_failure_jump (see above) that jumps to right
  4764.                  past a jump_past_next_alt.  */
  4765.  
  4766.               while ((enum regexpcode) p1[mcnt-3] == jump_past_next_alt)
  4767.                 {
  4768.                   /* MCNT holds how many bytes long the alternative
  4769.                      is, including the ending `jump_past_next_alt' and its number.  */
  4770.  
  4771.                   if (!alternative_can_match_nothing (p1, p1 + mcnt - 3, 
  4772.                                       reg_info))
  4773.                     return false;
  4774.  
  4775.                   /* Move to right after this alternative, including the
  4776.              jump_past_next_alt.  */
  4777.                      
  4778.                   p1 += mcnt;    
  4779.  
  4780.                   /* Break if it's the beginning of an n-th alternative
  4781.                      that doesn't begin with an on_failure_jump.  */
  4782.       
  4783.                   if ((enum regexpcode) *p1 != on_failure_jump)
  4784.                     break;
  4785.         
  4786.           /* Still have to check that it's not an n-th
  4787.              alternative that starts with an on_failure_jump.  */
  4788.           p1++;
  4789.                   extract_number_and_incr (&mcnt, &p1);
  4790.                   if ((enum regexpcode) p1[mcnt-3] != jump_past_next_alt)
  4791.                     {
  4792.               /* Get to the beginning of the n-th alternative.  */
  4793.                       p1 -= 3;
  4794.                       break;
  4795.                     }
  4796.                 }
  4797.  
  4798.               /* Deal with the last alternative: go back and get number
  4799.                  of the jump_past_next_alt just before it.  MCNT contains how
  4800.                  many bytes long the alternative is.  */
  4801.  
  4802.               mcnt = extract_number (p1 - 2);
  4803.  
  4804.               if (!alternative_can_match_nothing (p1, p1 + mcnt, reg_info))
  4805.                 return false;
  4806.  
  4807.               p1 += mcnt;    /* Get past the n-th alternative.  */
  4808.  
  4809.             } /*  if mcnt > 0  */
  4810.  
  4811.           break;
  4812.           
  4813.         case stop_memory:
  4814.           if (p1[1] == **p)
  4815.             {
  4816.               *p = p1 + 2;
  4817.               return true;
  4818.             }
  4819.           else
  4820.         {
  4821.               printf ("Error: encountered an unmatched (%d) stop_memory in \
  4822. group_can_match_nothing.\n", **p);
  4823.           exit (1);
  4824.         }          
  4825.       break;
  4826.     
  4827.           default: 
  4828.             if (!common_op_can_match_nothing (&p1, end, reg_info))
  4829.           return false;
  4830.         }
  4831.     }  /* While p1 < end.  */
  4832.  
  4833.   return false;
  4834. }
  4835.  
  4836.  
  4837. /* Similar to group_can_match_nothing, but doesn't deal with alternatives:
  4838.    It expects P to be the first byte of a single alternative and END one
  4839.    byte past the last. The alternative can contain groups.  */
  4840.    
  4841.  
  4842. static boolean
  4843. alternative_can_match_nothing (p, end, reg_info)
  4844.   unsigned char *p, *end;
  4845.   struct register_info *reg_info;
  4846. {
  4847.   int mcnt;
  4848.   unsigned char *p1 = p;
  4849.   
  4850.   while (p1 < end)
  4851.     {
  4852.       /* Skip over opcodes that can match nothing, and break when we get 
  4853.          to one that can't.  */
  4854.       
  4855.       switch ((enum regexpcode) *p1)
  4856.         {
  4857.     /* It's a loop.  */
  4858.         case on_failure_jump:
  4859.           p1++;
  4860.           extract_number_and_incr (&mcnt, &p1);
  4861.           p1 += mcnt;
  4862.           break;
  4863.           
  4864.     default: 
  4865.           if (!common_op_can_match_nothing (&p1, end, reg_info))
  4866.             return false;
  4867.         }
  4868.     }  /* While not at the end of the alternative.  */
  4869.  
  4870.   return true;
  4871. }
  4872.  
  4873.  
  4874. /* Deals with the ops common to group_can_match_nothing and
  4875.    alternative_can_match_nothing.  
  4876.    
  4877.    Sets P to one after the op and its arguments, if any.  */
  4878.  
  4879. static boolean
  4880. common_op_can_match_nothing (p, end, reg_info)
  4881.   unsigned char **p, *end;
  4882.   struct register_info *reg_info;
  4883. {
  4884.   int mcnt;
  4885.   unsigned char *p1 = *p;
  4886.   boolean ret;
  4887.   int reg_no;
  4888.  
  4889.   switch ((enum regexpcode) *p1++)
  4890.     {
  4891.     case no_op:
  4892.     case begline:
  4893.     case endline:
  4894.     case endline_in_repeat:
  4895.     case endline_before_newline:
  4896.       break;
  4897.  
  4898.     case start_memory:
  4899.       reg_no = *p1;
  4900.       ret = group_can_match_nothing (&p1, end, reg_info);
  4901.       
  4902.       /* Have to set this here in case we're checking a group which
  4903.          contains a group and a back reference to it.  */
  4904.  
  4905.       if (CAN_MATCH_NOTHING (reg_info[reg_no]) == -1)
  4906.          CAN_MATCH_NOTHING (reg_info[reg_no]) = ret;
  4907.  
  4908.       if (!ret)
  4909.         return false;
  4910.       break;
  4911.           
  4912.     /* If this is an optimized succeed_n for zero times, make the jump.  */
  4913.     case no_pop_jump:
  4914.       extract_number_and_incr (&mcnt, &p1);
  4915.       
  4916.       if (mcnt >= 0)
  4917.         p1 += mcnt;
  4918.       else
  4919.         return false;
  4920.       break;
  4921.  
  4922.     case succeed_n:
  4923.       /* Get to the number of times to succeed.  */
  4924.       p1 += 2;        
  4925.       extract_number_and_incr (&mcnt, &p1);
  4926.  
  4927.       if (mcnt == 0)
  4928.         {
  4929.           p1 -= 4;
  4930.           extract_number_and_incr (&mcnt, &p1);
  4931.           p1 += mcnt;
  4932.         }
  4933.       else
  4934.         return false;
  4935.       break;
  4936.  
  4937.     case duplicate: 
  4938.       if (!CAN_MATCH_NOTHING (reg_info[*p1]))
  4939.         return false;
  4940.       break;
  4941.  
  4942.     case set_number_at:
  4943.       p1 += 4;
  4944.     case before_dot:
  4945.     case at_dot:
  4946.     case after_dot:
  4947.     case begbuf:
  4948.     case endbuf:
  4949.     case wordbeg:
  4950.     case wordend:
  4951.     case wordbound:
  4952.     case notwordbound:
  4953.       break;
  4954.  
  4955.     default:
  4956.       /* All other opcodes mean we cannot match the empty string.  */
  4957.       return false;
  4958.   }
  4959.  
  4960.   *p = p1;
  4961.   return true;
  4962. }
  4963.  
  4964.  
  4965.  
  4966. /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
  4967.    bytes; nonzero otherwise.  */
  4968.    
  4969. static int
  4970. bcmp_translate (s1, s2, len, translate)
  4971.      unsigned char *s1, *s2;
  4972.      register int len;
  4973.      unsigned char *translate;
  4974. {
  4975.   register unsigned char *p1 = s1, *p2 = s2;
  4976.   while (len)
  4977.     {
  4978.       if (translate [*p1++] != translate [*p2++]) return 1;
  4979.       len--;
  4980.     }
  4981.   return 0;
  4982. }
  4983.  
  4984.  
  4985.  
  4986.  
  4987. /* Entry points compatible with 4.2 BSD regex library.  We don't define
  4988.    them if this is an Emacs or POSIX compilation.  */
  4989.  
  4990. #if !defined (emacs) && !defined (_POSIX_SOURCE)
  4991.  
  4992. static struct re_pattern_buffer re_comp_buf;
  4993.  
  4994. char *
  4995. re_comp (s)
  4996.   const char *s;
  4997. {
  4998.   char *return_value;
  4999.   
  5000.   if (!s)
  5001.     {
  5002.       if (!re_comp_buf.buffer)
  5003.     return "No previous regular expression";
  5004.       return 0;
  5005.     }
  5006.  
  5007.   if (!re_comp_buf.buffer)
  5008.     {
  5009.       re_comp_buf.buffer = (char *) malloc (200);
  5010.  
  5011.       if (re_comp_buf.buffer == NULL)
  5012.         return "Memory exhausted";
  5013.  
  5014.       re_comp_buf.allocated = 200;
  5015.  
  5016.       re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  5017.  
  5018.       if (re_comp_buf.fastmap == NULL)
  5019.     return "Memory exhausted";
  5020.     }
  5021.   return regex_compile (s, strlen (s), obscure_syntax, &re_comp_buf);
  5022. }
  5023.  
  5024. int
  5025. re_exec (s)
  5026.   const char *s;
  5027. {
  5028.   const int len = strlen (s);
  5029.   return 0 <= re_search (&re_comp_buf, s, len, 0, len, 
  5030.                  (struct re_registers *) 0);
  5031. }
  5032.  
  5033. #endif /* not emacs and not _POSIX_SOURCE */
  5034.  
  5035.  
  5036.  
  5037. /* Entry points compatible with POSIX regex library.  Only define these
  5038.    when this is a POSIX compilation (and it's not Emacs).  */
  5039.  
  5040. #ifndef emacs
  5041.  
  5042. /* regcomp takes a regular-expression string and converts it into a
  5043.    buffer full of byte commands for matching.
  5044.  
  5045.    PREG      is a regex_t * whose pertinent fields are mentioned in below:
  5046.                 
  5047.              It has a char * field called BUFFER which points to the
  5048.              space where this routine will put the compiled pattern; the
  5049.              user can either allocate this using malloc (whereupon they
  5050.              should set the long field ALLOCATED to the number of bytes
  5051.              malloced) or set ALLOCATED to 0 and let the routine
  5052.              allocate it.  The routine may use realloc to enlarge the
  5053.              buffer space.
  5054.              
  5055.              If the user wants to translate all ordinary elements in the
  5056.              compiled pattern, they should set the char * field
  5057.              TRANSLATE to a translate table (and not set the REG_ICASE
  5058.              bit of CFLAGS, which would override this translate table
  5059.              with one that ignores case); otherwise, they should set
  5060.              TRANSLATE to 0.
  5061.              
  5062.              The routine sets the int field SYNTAX to RE_SYNTAX_POSIX_EXTENDED
  5063.              if the REG_EXTENDED bit in CFLAGS is set; otherwise, it sets it 
  5064.              to RE_SYNTAX_POSIX_BASIC.
  5065.              
  5066.              It returns in the long field USED how many bytes long the
  5067.              compiled pattern is.
  5068.              
  5069.              It returns 0 in the char field FASTMAP_ACCURATE, on
  5070.              the assumption that the user usually doesn't compile the
  5071.              same pattern twice and that consequently any fastmap in the
  5072.              pattern buffer is inaccurate.
  5073.                    
  5074.          In the size_t field RE_NSUB, it returns the number of
  5075.              subexpressions it found in PATTERN.
  5076.  
  5077.    PATTERN   is the address of the pattern string.
  5078.  
  5079.    CFLAGS    is a series of bits ORed together which affect compilation.
  5080.              If the bit REG_EXTENDED is set, regcomp compiles the
  5081.              pattern as an extended regular expression, otherwise it
  5082.              compiles it as a basic one.  If the bit REG_NEWLINE is set,
  5083.              then dot and nonmatching lists won't match a newline, but
  5084.              pattern anchors will match at them.  If the bit REG_ICASE
  5085.              is set, then it considers upper- and lowercase versions of
  5086.              letters to be equal when matching.  If the bit REG_NOSUB is
  5087.              set, then when PREG is passed to regexec, that routine will
  5088.              only report success or failure.
  5089.  
  5090.  
  5091.    It returns 0 if it succeeds, nonzero if it doesn't.  (See regex.h for
  5092.    POSIX return codes and their meanings.)  */
  5093.  
  5094.  
  5095. int
  5096. regcomp (preg, pattern, cflags)
  5097.   regex_t *preg;
  5098.   const char *pattern; 
  5099.   int cflags;
  5100. {
  5101.   char *return_value;
  5102.   
  5103.   int syntax = cflags & REG_EXTENDED
  5104.            ? RE_SYNTAX_POSIX_EXTENDED
  5105.            : RE_SYNTAX_POSIX_BASIC;
  5106.  
  5107.   if (cflags & REG_NEWLINE)
  5108.     {
  5109.       syntax &= ~RE_DOT_NEWLINE;
  5110.       syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  5111.       syntax &= ~RE_NO_ANCHOR_AT_NEWLINE;
  5112.     }
  5113.  
  5114.   if (cflags & REG_ICASE)
  5115.     {
  5116.       unsigned i;
  5117.       
  5118.       preg->translate = (char *) malloc (CHAR_SET_SIZE);
  5119.  
  5120.       if (preg->translate == NULL)
  5121.         return REG_ESPACE;
  5122.  
  5123.       /* Map any uppercase characters into corresponding lowercase ones.  */
  5124.       for (i = 0; i < CHAR_SET_SIZE; i++)
  5125.         preg->translate[i] = isupper (i) ? tolower (i) : i;
  5126.     }
  5127.   else
  5128.     preg->translate = 0;
  5129.  
  5130.   preg->no_sub = cflags & REG_NOSUB;
  5131.  
  5132.   return_value = regex_compile (pattern, strlen (pattern), syntax, preg);
  5133.  
  5134.  
  5135.   if (return_value == 0)
  5136.     return 0;
  5137.   else if (strcmp (return_value, "Invalid regular expression") == 0)
  5138.     return REG_BADPAT;
  5139.   else if (strcmp (return_value, "Invalid character class name") == 0)
  5140.     return REG_ECTYPE;
  5141.   else if (strcmp (return_value, "Trailing backslash") == 0)
  5142.     return REG_EESCAPE;
  5143.   else if (strcmp (return_value, "Invalid back reference") == 0)
  5144.     return REG_ESUBREG;
  5145.   else if (strcmp (return_value, "Unmatched [ or [^") == 0)
  5146.     return REG_EBRACK;
  5147.   else if (strcmp (return_value, "Unmatched ( or \\(") == 0
  5148.            || strcmp (return_value, "Unmatched ) or \\)") == 0)
  5149.     return REG_EPAREN;
  5150.   else if (strcmp (return_value, "Unmatched \\{") == 0)
  5151.     return REG_EBRACE;
  5152.   else if (strcmp (return_value, "Invalid content of \\{\\}") == 0)
  5153.     return REG_BADBR;
  5154.   else if (strcmp (return_value, "Invalid range end") == 0)
  5155.     return REG_ERANGE;
  5156.   else if (strcmp (return_value, "Memory exhausted") == 0)
  5157.     return REG_ESPACE;
  5158.   else if (strcmp (return_value, "Invalid preceding regular expression") == 0
  5159.            || strcmp (return_value, 
  5160.               "Missing preceding regular expression") == 0)
  5161.     return REG_BADRPT;
  5162.  
  5163.   /* Codes added by GNU.  */
  5164.   
  5165.   else if (strcmp (return_value, "Premature end of regular expression") == 0)
  5166.     return REG_EEND;
  5167.   else if (strcmp (return_value, "Regular expression too big") == 0)
  5168.     return REG_ESIZE;
  5169. else
  5170.     return REG_BADPAT;
  5171. }
  5172.  
  5173.  
  5174. /* regexex matches a buffer full of byte commands for matching (gotten
  5175.    from compiling a regular expression) and matches it against a string.
  5176.    
  5177.    PREG      is a regex_t * whose pertinent fields are mentioned below:
  5178.                 
  5179.              It has a char * field called BUFFER which points to 
  5180.              the byte commands which make up the compiled pattern.
  5181.              
  5182.              Its char * field TRANSLATE, if not 0, translates all
  5183.              ordinary elements in the compiled pattern.
  5184.  
  5185.              Its int field SYNTAX is the syntax with which the pattern
  5186.              was compiled and hence should be matched with.
  5187.              
  5188.              The long field USED is how many bytes long the compiled
  5189.              pattern is.
  5190.              
  5191.              Its size_t field RE_NSUB contains how many subexpressions
  5192.              the pattern has.  (This may be useful for choosing a value
  5193.              for NMATCH).
  5194.              
  5195.          If its unsigned NO_SUB bit is set, then regexec will not
  5196.              return anything in PMATCH, but only report whether or not
  5197.              BUFFER matched STRING.
  5198.              
  5199.          Regardless of how its unsigned RETURN_DEFAULT_NUM_REGS bit
  5200.              is set, regexec only returns in PMATCH information about
  5201.              the whole pattern and NMATCH - 1 of its subexpressions.
  5202.              
  5203.    STRING    is the address of the string to be matched.
  5204.  
  5205.    NMATCH    is how many elements of PMATCH regex should fill.
  5206.  
  5207.    PMATCH    is an array of struct regex_t's.  If PREG's NO_SUB field
  5208.              isn't set, then regexec records in PMATCH[i], for i = 1 to
  5209.              PMATCH - 1, which substring of STRING matched the i-th
  5210.              subexpression of the regular expression compiled in BUFFER;
  5211.              it records in PMATCH[0] that information about all of
  5212.              STRING.  See the comment for `typedef struct...regmatch_t'
  5213.              in regex.h for more details.
  5214.  
  5215.              The caller must allocate PMATCH to have at least NMATCH
  5216.              elements. 
  5217.              
  5218.    EFLAGS    is two bits OR-ed together which affect execution.  If the
  5219.              bit REG_NOTBOL is set, then STRING's first character is not
  5220.              the beginning of a line; that means any beginning-of-line
  5221.              byte command in BUFFER won't match that first character.
  5222.              If the bit REG_NOTEOL is set, then a similar things holds
  5223.              for STRING's last character: it isn't the end of a line and
  5224.              any end-of-line byte command in BUFFER won't match it.
  5225.  
  5226.  
  5227.    It returns 0 if it matches and REG_NOMATCH if it doesn't.  */
  5228.  
  5229. int
  5230. regexec (preg, string, nmatch, pmatch, eflags)
  5231.   const regex_t *preg;
  5232.   const char *string; 
  5233.   size_t nmatch; 
  5234.   regmatch_t pmatch[]; 
  5235.   int eflags;
  5236. {
  5237.   int return_value;
  5238.   unsigned this_op;
  5239.   struct re_registers regs;
  5240.   regex_t private_preg;
  5241.   
  5242.   private_preg = *preg;
  5243.   private_preg.not_bol = eflags & REG_NOTBOL;
  5244.   private_preg.not_eol = eflags & REG_NOTEOL;
  5245.     
  5246.   private_preg.return_default_num_regs = 0;
  5247.   
  5248.   if (!private_preg.no_sub && nmatch > 0)
  5249.     {
  5250.       regs.num_regs = nmatch;
  5251.       regs.start = malloc (nmatch * sizeof (int));
  5252.       regs.end = malloc (nmatch * sizeof (int));
  5253.     }
  5254.   else
  5255.     {
  5256.       regs.num_regs = 0;
  5257.       regs.start = NULL;
  5258.       regs.end = NULL;
  5259.     }
  5260.  
  5261.   return_value = re_match (&private_preg, string, strlen (string), 0, 
  5262.                    !private_preg.no_sub && nmatch > 0 ? ®s : 0);
  5263.   
  5264.   if (return_value == strlen (string))
  5265.     {
  5266.       if (!private_preg.no_sub && nmatch > 0)
  5267.         {
  5268.           unsigned this_reg;
  5269.  
  5270.           for (this_reg = 0; this_reg < nmatch; this_reg++)
  5271.             {
  5272.               pmatch[this_reg].rm_so = regs.start[this_reg];
  5273.               pmatch[this_reg].rm_eo = regs.end[this_reg];
  5274.             }
  5275.         }
  5276.     }    
  5277.   if (regs.start != NULL)
  5278.     free (regs.start);
  5279.  
  5280.   if (regs.end != NULL)
  5281.     free (regs.end);
  5282.   
  5283.   return return_value == strlen (string) ? 0 : REG_NOMATCH;
  5284. }
  5285.  
  5286.  
  5287. /* Puts the first BUFFER_SIZE - 1 characters in BUFFER (if BUFFER isn't null)
  5288.    and terminates it with a null.
  5289.    
  5290.    Returns one more than the size of MESSAGE.  */
  5291.  
  5292. static size_t
  5293. put_in_buffer (message, buffer, buffer_size)
  5294.   char *message;
  5295.   char *buffer;
  5296.   size_t buffer_size;
  5297. {
  5298.   unsigned this_char;
  5299.   
  5300.   if (buffer != NULL && buffer_size > 0)
  5301.     {
  5302.       strncpy (buffer, message, buffer_size - 1);
  5303.       buffer[buffer_size - 1] = 0;
  5304.     }
  5305.  
  5306.   return strlen (message) + 1;
  5307. }  
  5308.  
  5309.  
  5310. /* Returns a message corresponding to an error code, ERRCODE, returned
  5311.    from either regcomp or regexec.   */
  5312.  
  5313. size_t
  5314. regerror (errcode, preg, errbuf, errbuf_size)
  5315.   int errcode;
  5316.   const regex_t *preg;
  5317.   char *errbuf;
  5318.   size_t errbuf_size;
  5319. {
  5320.     switch (errcode)
  5321.     {
  5322.       case REG_NOERROR: 
  5323.         return put_in_buffer ("Regex message: no error.", errbuf, errbuf_size); 
  5324.         
  5325.       case REG_NOMATCH: 
  5326.         return put_in_buffer ("Regex error: regexec didn't find a match.",
  5327.                   errbuf, errbuf_size); 
  5328.       case REG_BADPAT: 
  5329.         return put_in_buffer ("Regex error: Invalid regular expression.", 
  5330.                       errbuf, errbuf_size); 
  5331.       case REG_ECOLLATE: 
  5332.         return put_in_buffer ("Regex error: (not implemented) Invalid \
  5333. collating character.", errbuf, errbuf_size); 
  5334.         
  5335.       case REG_ECTYPE: 
  5336.         return put_in_buffer ("Regex error: Invalid character class name.",
  5337.                   errbuf, errbuf_size); 
  5338.       case REG_EESCAPE: 
  5339.         return put_in_buffer ("Regex error: Trailing backslash.",
  5340.                   errbuf, errbuf_size); 
  5341.       case REG_ESUBREG: 
  5342.         return put_in_buffer("Regex error: Invalid back reference.", 
  5343.                   errbuf, errbuf_size); 
  5344.       case REG_EBRACK: 
  5345.         return put_in_buffer ("Regex error: Unmatched [ or [^.",
  5346.                   errbuf, errbuf_size); 
  5347.       case REG_EPAREN: 
  5348.         return put_in_buffer ("Regex error: Unmatched parenthesis.",
  5349.                   errbuf, errbuf_size); 
  5350.       case REG_EBRACE: 
  5351.         return put_in_buffer ("Regex error: Unmatched \\{.",
  5352.                   errbuf, errbuf_size); 
  5353.       case REG_BADBR: 
  5354.         return put_in_buffer ("Regex error: Invalid content of \\{\\}.",
  5355.                           errbuf, errbuf_size); 
  5356.       case REG_ERANGE: 
  5357.         return put_in_buffer ("Regex error: Invalid range end.",
  5358.                   errbuf, errbuf_size); 
  5359.       case REG_ESPACE: 
  5360.         return put_in_buffer ("Regex error: Ran out of memory.", 
  5361.                   errbuf, errbuf_size); 
  5362.       case REG_BADRPT: 
  5363.         return put_in_buffer ("Regex error: Preceding regular expression \
  5364. either missing or not simple.", errbuf, errbuf_size); 
  5365.  
  5366.       case REG_EEND: 
  5367.         return put_in_buffer ("Regex error: Regular expression ended \
  5368. prematurely.", errbuf, errbuf_size); 
  5369.  
  5370.       case REG_ESIZE:
  5371.         return put_in_buffer ("Regex error: Excessively large regular \
  5372. expression.", errbuf, errbuf_size); 
  5373.     }
  5374. }
  5375.  
  5376.  
  5377. void
  5378. regfree (preg)
  5379.   regex_t *preg;
  5380. {
  5381.   if (preg->buffer != NULL)
  5382.     free (preg->buffer);
  5383.   preg->buffer = NULL;
  5384.   
  5385.   preg->allocated = 0;
  5386.   preg->used = 0;
  5387.  
  5388.   if (preg->fastmap != NULL)
  5389.     free (preg->fastmap);
  5390.   preg->fastmap = NULL;
  5391.  
  5392.   preg->fastmap_accurate = 0;
  5393.  
  5394.   if (preg->translate != NULL)
  5395.     free (preg->translate);
  5396.   preg->translate = NULL;
  5397. }
  5398.  
  5399. #endif /* not emacs  */
  5400.  
  5401.  
  5402.  
  5403.  
  5404. #ifdef test
  5405.  
  5406. #include <stdio.h>
  5407.  
  5408. /* Indexed by a character, gives the upper case equivalent of the
  5409.    character.  */
  5410.  
  5411. char upcase[0400] = 
  5412.   { 000, 001, 002, 003, 004, 005, 006, 007,
  5413.     010, 011, 012, 013, 014, 015, 016, 017,
  5414.     020, 021, 022, 023, 024, 025, 026, 027,
  5415.     030, 031, 032, 033, 034, 035, 036, 037,
  5416.     040, 041, 042, 043, 044, 045, 046, 047,
  5417.     050, 051, 052, 053, 054, 055, 056, 057,
  5418.     060, 061, 062, 063, 064, 065, 066, 067,
  5419.     070, 071, 072, 073, 074, 075, 076, 077,
  5420.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  5421.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  5422.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  5423.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  5424.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  5425.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  5426.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  5427.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  5428.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  5429.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  5430.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  5431.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  5432.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  5433.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  5434.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  5435.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  5436.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  5437.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  5438.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  5439.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  5440.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  5441.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  5442.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  5443.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  5444.   };
  5445.  
  5446. #ifdef canned
  5447.  
  5448. #include "tests.h"
  5449.  
  5450. typedef enum {extended_test, basic_test, other_test, interface_test} test_type;
  5451.  
  5452. /* Use this to run the tests we've thought of.  */
  5453.  
  5454. void
  5455. main ()
  5456. {
  5457.   test_type t = interface_test;
  5458.   
  5459.   if (t == basic_test)
  5460.     test_posix_basic ();
  5461.   else if (t == extended_test)
  5462.     test_posix_extended (); 
  5463.   else if (t == other_test)
  5464.     test_others (); 
  5465.   else if (t == interface_test)
  5466.     test_posix_c_interface (); 
  5467.  
  5468.   exit (0);
  5469. }
  5470.  
  5471. #else /* not canned */
  5472.  
  5473. /* Use this to run interactive tests.  */
  5474.  
  5475. void
  5476. main (argc, argv)
  5477.      int argc;
  5478.      char **argv;
  5479. {
  5480.   char pat[80];
  5481.   struct re_pattern_buffer buf;
  5482.   int i;
  5483.   char c;
  5484.   char fastmap[(1 << BYTEWIDTH)];
  5485.  
  5486.   /* Allow a command argument to specify the style of syntax.  */
  5487.   if (argc > 1)
  5488.     re_set_syntax (atoi (argv[1]));
  5489.  
  5490.   buf.allocated = 40;
  5491.   buf.buffer = (char *) malloc (buf.allocated);
  5492.   buf.fastmap = fastmap;
  5493.   buf.translate = upcase;
  5494.  
  5495.   while (1)
  5496.     {
  5497.       gets (pat);
  5498.  
  5499.       if (*pat)
  5500.     {
  5501.           re_compile_pattern (pat, strlen(pat), &buf);
  5502.  
  5503.       for (i = 0; i < buf.used; i++)
  5504.         printchar (buf.buffer[i]);
  5505.  
  5506.       putchar ('\n');
  5507.  
  5508.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  5509.  
  5510.       re_compile_fastmap (&buf);
  5511.       printf ("Allowed by fastmap: ");
  5512.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  5513.         if (fastmap[i]) printchar (i);
  5514.       putchar ('\n');
  5515.     }
  5516.  
  5517.       gets (pat);    /* Now read the string to match against */
  5518.  
  5519.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  5520.       printf ("Match value %d.\n", i);
  5521.     }
  5522. }
  5523.  
  5524. #endif
  5525.  
  5526.  
  5527. #if 0
  5528. print_buf (bufp)
  5529.      struct re_pattern_buffer *bufp;
  5530. {
  5531.   int i;
  5532.  
  5533.   printf ("buf is :\n----------------\n");
  5534.   for (i = 0; i < bufp->used; i++)
  5535.     printchar (bufp->buffer[i]);
  5536.   
  5537.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  5538.   
  5539.   printf ("Allowed by fastmap: ");
  5540.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  5541.     if (bufp->fastmap[i])
  5542.       printchar (i);
  5543.   printf ("\nAllowed by translate: ");
  5544.   if (bufp->translate)
  5545.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  5546.       if (bufp->translate[i])
  5547.     printchar (i);
  5548.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  5549.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  5550. }
  5551. #endif /* 0 */
  5552.  
  5553.  
  5554. printchar (c)
  5555.      char c;
  5556. {
  5557.   if (c < 040 || c >= 0177)
  5558.     {
  5559.       putchar ('\\');
  5560.       putchar (((c >> 6) & 3) + '0');
  5561.       putchar (((c >> 3) & 7) + '0');
  5562.       putchar ((c & 7) + '0');
  5563.     }
  5564.   else
  5565.     putchar (c);
  5566. }
  5567.  
  5568. error (string)
  5569.      char *string;
  5570. {
  5571.   puts (string);
  5572.   exit (1);
  5573. }
  5574. #endif /* test */
  5575.  
  5576.  
  5577.  
  5578. /*
  5579. Local variables:
  5580. make-backup-files: t
  5581. version-control: t
  5582. trim-versions-without-asking: nil
  5583. End:
  5584. */
  5585.